Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 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("test/" + 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.  
  113.     DoInput( inputFile);
  114.  
  115.     ofstream out("test/" + inputFile + ".out", ios::out);
  116.  
  117.     std::sort(libraries.begin(), libraries.end(),
  118.         [](const Library& lhs, const Library& rhs) {return LibraryH(lhs) > LibraryH(rhs); }
  119.     );
  120.  
  121.     int nextSignFreeDay = 0;
  122.     int nextSignLib = 0;
  123.  
  124.     vector<int> signedLibs;
  125.     vector<LibOutput> output(L);
  126.  
  127.     for (int day = 0; day < D; ++day)
  128.     {
  129.         if (day >= nextSignFreeDay)
  130.         {
  131.             //signedLibs.push_back(nextSignLib);
  132.             output[nextSignLib].LibId = libraries[nextSignLib].Id;
  133.             int offset = libraries[nextSignLib].SignUpTime;
  134.  
  135.             signedLibs.push_back(nextSignLib++);
  136.             nextSignFreeDay += offset;
  137.         }
  138.  
  139.         for (size_t i = 0; i < signedLibs.size(); i++)
  140.         {
  141.             int bookToScan = libraries[signedLibs[i]].ScanBook();
  142.             output[i].Books.push_back(bookToScan);
  143.             bookScanned[bookToScan] = true;
  144.         }
  145.     }
  146.  
  147.     for (auto& outputEntry : output)
  148.     {
  149.         outputEntry.Write(out);
  150.     }
  151.  
  152.     out.close();
  153.  
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement