-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbook_scanning.cpp
executable file
·133 lines (112 loc) · 2.83 KB
/
book_scanning.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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <utility>
#include <algorithm>
using namespace std;
/*
B - the number of different books
L - the number of libraries
D - the number of days
S - the scores of individual books
N - the number of books in library i
T - the number of days for signup for library i
M - the number of books that can be shipped for library i
ID - the id books for library i
Y - the id of scanning the library
K - the book id for scanning from library i
*/
// global variables
int B, L, D;
double w1, w2, w3;
unordered_map<int, int> score;
unordered_map<int, bool> done, ldone;
int S[100000], N[100000], T[100000], M[100000];
void sort_books(int l, vector<vector<int>>& ID) {
sort(ID[l].begin(), ID[l].end(), [](int& l, int& r) {
return score[l] < score[r];
});
}
int sort_libraries(vector<vector<int>>& ID) {
pair<int, int> library;
vector<pair<double, int>> libraries;
for (int i = 0; i < L; i++)
if (!ldone.count(i)) {
long long s = 0;
for (int j = 0; j < ID[i].size(); j++)
if (!done.count(ID[i][j]))
s += score[ID[i][j]];
library.first = w1 * (1.0 / T[i]) + w2 * M[i] + w3 * s;
library.second = i;
libraries.push_back(library);
}
if (libraries.empty())
return -1;
sort(libraries.rbegin(), libraries.rend());
ldone[libraries.front().second] = true;
return libraries.front().second;
}
void book_scanning(vector<vector<int>>& ID) {
int l;
long long s = 0;
vector<int> Y;
vector<vector<int>> K;
// initialize book score in map for fast-lookup
for (int i = 0; i < B; i++)
score[i] = S[i];
// consider libraries as long as we have enough days
while (D > 0) {
l = sort_libraries(ID);
if (l == -1)
break;
D -= T[l];
int d = D;
vector<int> b;
sort_books(l, ID);
while (!ID[l].empty()) {
int book = ID[l].back();
if (!done.count(book)) {
done[book] = true;
b.push_back(book);
s += score[book];
}
ID[l].pop_back();
}
if (!b.empty()) {
Y.push_back(l);
K.push_back(b);
}
}
// output solution
cout << Y.size() << endl;
for (int i = 0; i < Y.size(); i++) {
cout << Y[i] << " " << K[i].size() << endl;
for (int j = 0; j < K[i].size(); j++)
cout << K[i][j] << " ";
cout << endl;
}
}
int main(int argc, char **argv) {
// fast input & output
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// file input & output
string f = argv[1];
w1 = stod(argv[2]); // signup
w2 = stod(argv[3]); // shipping
w3 = stod(argv[4]); // score of books
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
cin >> B >> L >> D;
vector<vector<int>> ID(L);
for (int i = 0; i < B; i++)
cin >> S[i];
for (int i = 0; i < L; i++) {
cin >> N[i] >> T[i] >> M[i];
ID[i] = vector<int>(N[i]);
for (int j = 0; j < N[i]; j++)
cin >> ID[i][j];
}
book_scanning(ID);
return 0;
}