Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <algorithm>
  2. #include <queue>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. vector<int> bookWeights;
  9. vector<bool> bookScanned;
  10.  
  11. struct Library
  12. {
  13.     vector<int> Books;
  14.     int SignUpTime;
  15.     int BooksPerDay;
  16.     int TotalScore;
  17.     int Id;
  18.  
  19.     void Init()
  20.     {
  21.         std::sort(Books.begin(), Books.end(), [](int lbi, int rbi) {return bookWeights[lbi] > bookWeights[rbi]; });
  22.     }
  23.  
  24.     int ScanBook()
  25.     {
  26.         while (nextScanBookI < Books.size() && bookScanned[nextScanBookI])
  27.         {
  28.             ++nextScanBookI;
  29.         }
  30.  
  31.         return nextScanBookI == Books.size() ? -1 : nextScanBookI;
  32.     }
  33.  
  34. private:
  35.     int nextScanBookI = 0;
  36. };
  37.  
  38. vector<Library> libraries;
  39.  
  40. int B, L, D;
  41.  
  42. int LibraryH(const Library& library)
  43. {
  44.     return (library.TotalScore * library.SignUpTime) / library.BooksPerDay;
  45. }
  46.  
  47. struct LibOutput
  48. {
  49.     int LibId;
  50.     vector<int> Books;
  51.  
  52.     void Write(std::ostream& out) const
  53.     {
  54.         out << LibId << " " << Books.size() << endl;
  55.         for (int bookId : Books)
  56.             out << bookId << " ";
  57.         out << endl;
  58.     }
  59. };
  60.  
  61. void DoInput(string& inputFile)
  62. {
  63.     cin >> inputFile;
  64.  
  65.     ifstream in(inputFile + ".txt", ios::in);
  66.  
  67.     if (in.is_open() == false) {
  68.  
  69.         cout << "Couldn't open the file!" << endl;
  70.         return;
  71.     }
  72.  
  73.     in >> B;
  74.     in >> L;
  75.     in >> D;
  76.  
  77.     int input;
  78.  
  79.     //resize vectors
  80.     bookWeights.resize(B);
  81.     libraries.resize(L);
  82.  
  83.     for (size_t i = 0; i < B; i++)
  84.     {
  85.         in >> bookWeights[i];
  86.     }
  87.  
  88.     for (size_t i = 0; i < L; i++)
  89.     {
  90.         Library lib;
  91.  
  92.         in >> input;
  93.         lib.Books.resize(input);
  94.         in >> lib.SignUpTime;
  95.         in >> lib.BooksPerDay;
  96.  
  97.         for (size_t j = 0; j < input; j++)
  98.         {
  99.             in >> lib.Books[j];
  100.         }
  101.  
  102.         lib.Id = i;
  103.         libraries[i] = std::move(lib);
  104.     }
  105.  
  106.     in.close();
  107. }
  108.  
  109. int main()
  110. {
  111.     string inputFile;
  112.     DoInput(inputFile);
  113.  
  114.     ofstream out(inputFile + ".out", ios::out);
  115.  
  116.  
  117.     bookScanned.resize(B, false);
  118.  
  119.     std::sort(libraries.begin(), libraries.end(),
  120.         [](const Library& lhs, const Library& rhs) {return LibraryH(lhs) > LibraryH(rhs); }
  121.     );
  122.  
  123.     int nextSignFreeDay = 0;
  124.     int nextSignLib = 0;
  125.  
  126.     vector<int> signedLibs;
  127.     vector<LibOutput> output(L);
  128.  
  129.     for (int day = 0; day < D; ++day)
  130.     {
  131.         if (nextSignLib < libraries.size() && day >= nextSignFreeDay)
  132.         {
  133.             //signedLibs.push_back(nextSignLib);
  134.             output[nextSignLib].LibId = libraries[nextSignLib].Id;
  135.             int offset = libraries[nextSignLib].SignUpTime;
  136.  
  137.             signedLibs.push_back(nextSignLib++);
  138.             nextSignFreeDay += offset;
  139.         }
  140.  
  141.         for (size_t i = 0; i < signedLibs.size(); i++)
  142.         {
  143.             int bookToScan = libraries[signedLibs[i]].ScanBook();
  144.             if (bookToScan < 0)
  145.                 continue;
  146.             output[i].Books.push_back(bookToScan);
  147.             bookScanned[bookToScan] = true;
  148.         }
  149.     }
  150.  
  151.  
  152.     int totalBooks = 0;
  153.     for (auto flag : bookScanned)
  154.     {
  155.         if (flag)
  156.             totalBooks++;
  157.     }
  158.     out << nextSignLib << " " << totalBooks << endl;
  159.  
  160.     for (auto& outputEntry : output)
  161.     {
  162.         outputEntry.Write(out);
  163.     }
  164.  
  165.     out.close();
  166.  
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement