Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 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("~/FMI/HashCode/test/" + inputFile + ".txt");
  66.  
  67.     if (!in.is_open()) {
  68.         cout << "Couldn't open the file!" << endl;
  69.     }
  70.  
  71.     in >> B;
  72.     in >> L;
  73.     in >> D;
  74.  
  75.     int input;
  76.  
  77.     //resize vectors
  78.     bookWeights.resize(B);
  79.     libraries.resize(L);
  80.  
  81.     for (size_t i = 0; i < B; i++)
  82.     {
  83.         in >> bookWeights[i];
  84.     }
  85.  
  86.     for (size_t i = 0; i < L; i++)
  87.     {
  88.         Library lib;
  89.  
  90.         in >> input;
  91.         lib.Books.resize(input);
  92.         in >> lib.SignUpTime;
  93.         in >> lib.BooksPerDay;
  94.  
  95.         for (size_t j = 0; j < input; j++)
  96.         {
  97.             in >> lib.Books[j];
  98.         }
  99.  
  100.         lib.Id = i;
  101.         libraries[i] = std::move(lib);
  102.     }
  103.  
  104.     in.close();
  105. }
  106.  
  107. int main()
  108. {
  109.     string inputFile;
  110.  
  111.     ofstream out("~/FMI/HashCode/test/" + inputFile + ".out");
  112.  
  113.     DoInput( inputFile);
  114.  
  115.     std::sort(libraries.begin(), libraries.end(),
  116.         [](const Library& lhs, const Library& rhs) {return LibraryH(lhs) > LibraryH(rhs); }
  117.     );
  118.  
  119.     int nextSignFreeDay = 0;
  120.     int nextSignLib = 0;
  121.  
  122.     vector<int> signedLibs;
  123.     vector<LibOutput> output(L);
  124.  
  125.     for (int day = 0; day < D; ++day)
  126.     {
  127.         if (day >= nextSignFreeDay)
  128.         {
  129.             //signedLibs.push_back(nextSignLib);
  130.             output[nextSignLib].LibId = libraries[nextSignLib].Id;
  131.             int offset = libraries[nextSignLib].SignUpTime;
  132.  
  133.             signedLibs.push_back(nextSignLib++);
  134.             nextSignFreeDay += offset;
  135.         }
  136.  
  137.         for (size_t i = 0; i < signedLibs.size(); i++)
  138.         {
  139.             int bookToScan = libraries[signedLibs[i]].ScanBook();
  140.             output[i].Books.push_back(bookToScan);
  141.             bookScanned[bookToScan] = true;
  142.         }
  143.     }
  144.  
  145.     for (auto& outputEntry : output)
  146.     {
  147.         outputEntry.Write(out);
  148.     }
  149.  
  150.     out.close();
  151.  
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement