Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <queue>
- #include <iostream>
- #include <fstream>
- using namespace std;
- vector<int> bookWeights;
- vector<bool> bookScanned;
- struct Library
- {
- vector<int> Books;
- int SignUpTime;
- int BooksPerDay;
- int TotalScore;
- int Id;
- void Init()
- {
- std::sort(Books.begin(), Books.end(), [](int lbi, int rbi) {return bookWeights[lbi] > bookWeights[rbi]; });
- }
- int ScanBook()
- {
- while (nextScanBookI < Books.size() && bookScanned[nextScanBookI])
- {
- ++nextScanBookI;
- }
- return nextScanBookI == Books.size() ? -1 : nextScanBookI;
- }
- private:
- int nextScanBookI = 0;
- };
- vector<Library> libraries;
- int B, L, D;
- int LibraryH(const Library& library)
- {
- return (library.TotalScore * library.SignUpTime) / library.BooksPerDay;
- }
- struct LibOutput
- {
- int LibId;
- vector<int> Books;
- void Write(std::ostream& out) const
- {
- out << LibId << " " << Books.size() << endl;
- for (int bookId : Books)
- out << bookId << " ";
- out << endl;
- }
- };
- void DoInput(string& inputFile)
- {
- cin >> inputFile;
- ifstream in("~/FMI/HashCode/test/" + inputFile + ".txt");
- if (!in.is_open()) {
- cout << "Couldn't open the file!" << endl;
- }
- in >> B;
- in >> L;
- in >> D;
- int input;
- //resize vectors
- bookWeights.resize(B);
- libraries.resize(L);
- for (size_t i = 0; i < B; i++)
- {
- in >> bookWeights[i];
- }
- for (size_t i = 0; i < L; i++)
- {
- Library lib;
- in >> input;
- lib.Books.resize(input);
- in >> lib.SignUpTime;
- in >> lib.BooksPerDay;
- for (size_t j = 0; j < input; j++)
- {
- in >> lib.Books[j];
- }
- lib.Id = i;
- libraries[i] = std::move(lib);
- }
- in.close();
- }
- int main()
- {
- string inputFile;
- ofstream out("~/FMI/HashCode/test/" + inputFile + ".out");
- DoInput( inputFile);
- std::sort(libraries.begin(), libraries.end(),
- [](const Library& lhs, const Library& rhs) {return LibraryH(lhs) > LibraryH(rhs); }
- );
- int nextSignFreeDay = 0;
- int nextSignLib = 0;
- vector<int> signedLibs;
- vector<LibOutput> output(L);
- for (int day = 0; day < D; ++day)
- {
- if (day >= nextSignFreeDay)
- {
- //signedLibs.push_back(nextSignLib);
- output[nextSignLib].LibId = libraries[nextSignLib].Id;
- int offset = libraries[nextSignLib].SignUpTime;
- signedLibs.push_back(nextSignLib++);
- nextSignFreeDay += offset;
- }
- for (size_t i = 0; i < signedLibs.size(); i++)
- {
- int bookToScan = libraries[signedLibs[i]].ScanBook();
- output[i].Books.push_back(bookToScan);
- bookScanned[bookToScan] = true;
- }
- }
- for (auto& outputEntry : output)
- {
- outputEntry.Write(out);
- }
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement