Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int score(vector<int> &books, vector<int> &scores, int t, int m, int d) {
  6.  
  7.     vector<pair<int, int>> book_scores(books.size());
  8.     for (int i = 0; i < books.size(); i++) {
  9.         book_scores[i] = {scores[books[i]], books[i]};
  10.     }
  11.     sort(book_scores.begin(), book_scores.end());
  12.     int num_books = (d - t) * m;
  13.     int lib_score = 0;
  14.     //cerr << "num " << num_books;
  15.     for (int i = books.size() - 1; books.size() - i <= num_books && i >= 0; i--) {
  16.         lib_score += book_scores[i].first;
  17.         //cerr << lib_score << " score \n";
  18.     }
  19.     //cerr << "num " << num_books;
  20.     return lib_score;
  21. }
  22.  
  23. int main()
  24. {
  25.  
  26.     //ifstream fileIn("a_example.txt");
  27.     //ifstream fileIn("b_read_on.txt");
  28.     //ifstream fileIn("c_incunabula.txt");
  29.     //ifstream fileIn("d_tough_choices.txt");
  30.     //ifstream fileIn("e_so_many_books.txt");
  31.     ifstream fileIn("f_libraries_of_the_world.txt");
  32.     ofstream fileOut("output_f.txt");
  33.  
  34.     int B, L, D; // books, libraries, days
  35.     fileIn >> B >> L >> D;
  36.     vector<int> S(B); // scores
  37.     for (int i = 0; i < B; i++) {
  38.         fileIn >> S[i];
  39.     }
  40.     vector<int> N(L); // num books
  41.     vector<int> T(L); // signup
  42.     vector<int> M(L); // books/day
  43.     vector<vector<int>> books(L);
  44.     for (int i = 0; i < L; i++) {
  45.         fileIn >> N[i] >> T[i] >> M[i];
  46.         for (int j = 0; j < N[i]; j++) {
  47.             int b;
  48.             fileIn >> b;
  49.             books[i].push_back(b);
  50.         }
  51.     }
  52.     vector<pair<int, int>> scored_libs(L);
  53.     for (int i = 0; i < L; i++) {
  54.         scored_libs[i] = {score(books[i], S, T[i], M[i], D), i};
  55.     }
  56.     sort(scored_libs.begin(), scored_libs.end());
  57.  
  58.     vector<bool> sent(B, false);
  59.     int time = D;
  60.  
  61.     vector<int> lib_chosen;
  62.     vector<vector<int>> books_chosen;
  63.  
  64.     cerr << "Libs: " << L << "\n";
  65.  
  66.     for (int i = L - 1; i >= 0 && time > 0; i--) {
  67.         int lib = scored_libs[i].second;
  68.         time -= T[lib];
  69.  
  70.         //cerr << time << "\n";
  71.  
  72.         if (time <= 0) {
  73.             continue;
  74.         }
  75.  
  76.         cerr << i << "\n";
  77.  
  78.         int num_books = time * M[lib];
  79.         vector<pair<int, int>> book_scores(books[lib].size());
  80.         for (int j = 0; j < books[lib].size(); j++) {
  81.             book_scores[j] = make_pair(S[books[lib][j]], books[lib][j]);
  82.         }
  83.         sort(book_scores.begin(), book_scores.end());
  84.  
  85.         vector<int> books_to_send;
  86.  
  87.         int num_sent = 0;
  88.  
  89.         for (int j = books[lib].size() - 1; j >= 0; j--) {
  90.             int my_book = book_scores[j].second;
  91.             if (sent[my_book]) {
  92.                 continue;
  93.             }
  94.             sent[my_book] = true;
  95.             num_sent++;
  96.             books_to_send.push_back(my_book);
  97.         }
  98.  
  99.         if (num_sent > 0) {
  100.             lib_chosen.push_back(lib);
  101.             books_chosen.push_back(books_to_send);
  102.         }
  103.     }
  104.     fileOut << lib_chosen.size() << "\n";
  105.     for (int i = 0; i < lib_chosen.size(); i++) {
  106.         fileOut << lib_chosen[i] << " " << books_chosen[i].size() << "\n";
  107.         for (int j = 0; j < books_chosen[i].size(); j++) {
  108.             fileOut << books_chosen[i][j] << " ";
  109.         }
  110.         fileOut << "\n";
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement