Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int score(vector<int> &books, vector<int> &scores, int t, int m, int d) {
- vector<pair<int, int>> book_scores(books.size());
- for (int i = 0; i < books.size(); i++) {
- book_scores[i] = {scores[books[i]], books[i]};
- }
- sort(book_scores.begin(), book_scores.end());
- int num_books = (d - t) * m;
- int lib_score = 0;
- //cerr << "num " << num_books;
- for (int i = books.size() - 1; books.size() - i <= num_books && i >= 0; i--) {
- lib_score += book_scores[i].first;
- //cerr << lib_score << " score \n";
- }
- //cerr << "num " << num_books;
- return lib_score;
- }
- int main()
- {
- //ifstream fileIn("a_example.txt");
- //ifstream fileIn("b_read_on.txt");
- //ifstream fileIn("c_incunabula.txt");
- //ifstream fileIn("d_tough_choices.txt");
- //ifstream fileIn("e_so_many_books.txt");
- ifstream fileIn("f_libraries_of_the_world.txt");
- ofstream fileOut("output_f.txt");
- int B, L, D; // books, libraries, days
- fileIn >> B >> L >> D;
- vector<int> S(B); // scores
- for (int i = 0; i < B; i++) {
- fileIn >> S[i];
- }
- vector<int> N(L); // num books
- vector<int> T(L); // signup
- vector<int> M(L); // books/day
- vector<vector<int>> books(L);
- for (int i = 0; i < L; i++) {
- fileIn >> N[i] >> T[i] >> M[i];
- for (int j = 0; j < N[i]; j++) {
- int b;
- fileIn >> b;
- books[i].push_back(b);
- }
- }
- vector<pair<int, int>> scored_libs(L);
- for (int i = 0; i < L; i++) {
- scored_libs[i] = {score(books[i], S, T[i], M[i], D), i};
- }
- sort(scored_libs.begin(), scored_libs.end());
- vector<bool> sent(B, false);
- int time = D;
- vector<int> lib_chosen;
- vector<vector<int>> books_chosen;
- cerr << "Libs: " << L << "\n";
- for (int i = L - 1; i >= 0 && time > 0; i--) {
- int lib = scored_libs[i].second;
- time -= T[lib];
- //cerr << time << "\n";
- if (time <= 0) {
- continue;
- }
- cerr << i << "\n";
- int num_books = time * M[lib];
- vector<pair<int, int>> book_scores(books[lib].size());
- for (int j = 0; j < books[lib].size(); j++) {
- book_scores[j] = make_pair(S[books[lib][j]], books[lib][j]);
- }
- sort(book_scores.begin(), book_scores.end());
- vector<int> books_to_send;
- int num_sent = 0;
- for (int j = books[lib].size() - 1; j >= 0; j--) {
- int my_book = book_scores[j].second;
- if (sent[my_book]) {
- continue;
- }
- sent[my_book] = true;
- num_sent++;
- books_to_send.push_back(my_book);
- }
- if (num_sent > 0) {
- lib_chosen.push_back(lib);
- books_chosen.push_back(books_to_send);
- }
- }
- fileOut << lib_chosen.size() << "\n";
- for (int i = 0; i < lib_chosen.size(); i++) {
- fileOut << lib_chosen[i] << " " << books_chosen[i].size() << "\n";
- for (int j = 0; j < books_chosen[i].size(); j++) {
- fileOut << books_chosen[i][j] << " ";
- }
- fileOut << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement