Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <queue>
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- vector<int> bookWeights;
- vector<bool> bookScanned;
- struct Library
- {
- vector<int> Books;
- int SignUpTime;
- int BooksPerDay;
- int TotalScore;
- int Id;
- int StartDay;
- void Init()
- {
- std::sort(Books.begin(), Books.end(), [](int lbi, int rbi) {return bookWeights[lbi] > bookWeights[rbi]; });
- }
- int ScanBook()
- {
- while (nextScanBookI < Books.size() && bookScanned[Books[nextScanBookI]])
- {
- ++nextScanBookI;
- }
- return nextScanBookI >= Books.size() ? -1 : Books[nextScanBookI];
- }
- inline bool HasMoreBooks() const { return nextScanBookI < Books.size(); }
- 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(inputFile + ".txt", ios::in);
- if (in.is_open() == false) {
- cout << "Couldn't open the file!" << endl;
- return;
- }
- in >> B;
- in >> L;
- in >> D;
- int input;
- //resize vectors
- bookWeights.resize(B);
- libraries.reserve(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.reserve(input);
- in >> lib.SignUpTime;
- in >> lib.BooksPerDay;
- long long totalScore = 0;
- for (size_t j = 0; j < input; j++)
- {
- int bookId;
- in >> bookId;
- totalScore += bookWeights[bookId];
- lib.Books.push_back(bookId);
- }
- lib.TotalScore = totalScore;
- lib.Id = i;
- libraries.push_back(std::move(lib));
- }
- in.close();
- }
- int main()
- {
- string inputFile;
- DoInput(inputFile);
- ofstream out(inputFile + ".out", ios::out);
- bookScanned.resize(B, false);
- std::sort(libraries.begin(), libraries.end(),
- [](const Library& lhs, const Library& rhs) {return LibraryH(lhs) > LibraryH(rhs); }
- );
- int nextSignFreeDay = 0;
- int signedLibsCount = 0;
- for (auto& lib : libraries)
- {
- lib.StartDay = nextSignFreeDay + lib.SignUpTime;
- nextSignFreeDay += lib.SignUpTime;
- ++signedLibsCount;
- if (nextSignFreeDay >= D)
- break;
- }
- vector<LibOutput> output(signedLibsCount);
- for (int i = 0; i < signedLibsCount; ++i)
- output[i].LibId = libraries[i].Id;
- for (int day = 0; day < D; ++day)
- {
- for(int libIdx = 0; libIdx<signedLibsCount; ++libIdx)
- {
- auto& lib = libraries[libIdx];
- if (day < lib.StartDay)
- break;
- if (lib.HasMoreBooks())
- {
- for (int scanI = 0; scanI < lib.BooksPerDay; ++scanI)
- {
- auto bookToScan = lib.ScanBook();
- if (bookToScan >= 0)
- {
- output[libIdx].Books.push_back(bookToScan);
- bookScanned[bookToScan] = true;
- }
- else
- {
- break;
- }
- }
- }
- }
- }
- int totalLibs = 0;
- for (const auto& outEntry : output)
- {
- if (outEntry.Books.size() > 0)
- totalLibs++;
- }
- out << totalLibs << endl;
- for (const auto& outEntry : output)
- {
- if(outEntry.Books.size() > 0)
- outEntry.Write(out);
- }
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement