-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
163 lines (133 loc) · 3.12 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
using int_vec = vector<int>;
int library_id_count = 0;
struct Library {
int id;
int N;
int T;
int M;
int_vec books;
};
struct lib_ratio
{
Library l;
int ratio;
};
using lib_vectratio = vector<lib_ratio>;
bool sort_lib(lib_ratio lhs, lib_ratio rhs)
{
return lhs.ratio > rhs.ratio;
}
istream& operator>>(istream &in, Library &l)
{
l.id = library_id_count++;
in >> l.N >> l.T >> l.M;
l.books.resize(l.N);
for (auto &book: l.books)
in >> book;
return in;
}
ostream& operator<<(ostream &out, Library &l) {
out << "Library #" << l.id << " {" << endl;
out << "N: " << l.N << endl;
out << "T: " << l.T << endl;
out << "M: " << l.M << endl;
out << "Books:" << endl;
for (auto& book: l.books)
out << book << " ";
out << endl << "}" << endl;
return out;
}
struct Output {
vector<Library> libraries;
vector<int_vec> books;
void add_library(const Library &l, const int_vec &books);
void dump() const;
};
void Output::add_library(const Library &l, const int_vec &b) {
libraries.push_back(l);
books.push_back(b);
}
void Output::dump() const {
cout << libraries.size() << endl;
for (auto i = 0u; i < libraries.size(); i++)
{
cout << libraries[i].id << " " << books[i].size() << endl;
for (auto &b: books[i])
cout << b << " ";
cout << endl;
}
}
vector<Library> libraries;
int B;
int L;
int D;
int_vec book_scores;
int book_val(Library l, int left_books)
{
int value = 0;
for(int i = 0; i < left_books && i < l.books.size(); i++)
value += book_scores[l.books[i]];
return value;
}
int curr_days = 0;
bool basic_sort(int a, int b)
{
return book_scores[a] > book_scores[b];
}
int ratio(Library l)
{
if (l.T > D- curr_days)
return -1;
sort(l.books.begin(), l.books.end(), basic_sort);
int left_books = (D - curr_days) * l.M;
int a = book_val(l, D - curr_days);
return l.N * l.M * a - l.T;
}
void add_ratio(Output o)
{
lib_vectratio lib_ratios;
for(auto l : libraries)
{
lib_ratio lr { l, ratio(l)};
lib_ratios.push_back(lr);
}
sort(lib_ratios.begin(), lib_ratios.end(), sort_lib);
while(!lib_ratios.empty())
{
o.add_library(lib_ratios[0].l, lib_ratios[0].l.books);
curr_days += lib_ratios[0].l.T;
lib_ratios.erase(lib_ratios.begin());
for(auto lr : lib_ratios)
{
lr.ratio = ratio(lr.l);
}
sort(lib_ratios.begin(), lib_ratios.end(), sort_lib);
}
/*
for(auto lr : lib_ratios)
{
o.add_library(lr.l, lr.l.books);
if (D == 1)
break;
D--;
}*/
o.dump();
}
int main() {
cin >> B >> L >> D;
book_scores.resize(B);
for (auto &book_score: book_scores)
cin >> book_score;
libraries.resize(L);
for (auto &l: libraries)
cin >> l;
Output o;
add_ratio(o);
//o.add_library(libraries[0], libraries[0].books);
//o.add_library(libraries[1], libraries[1].books);
return 0;
}