Advertisement
Guest User

Untitled

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