Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <deque>
- #include <map>
- #include <string>
- #include <algorithm>
- #include <cstdlib>
- using namespace std;
- struct date {
- int day, duration, k, begTime = 0, endTime = 0;
- char strTime[6];
- vector <string> names;
- void SetTime() {
- begTime = atoi(strTime)*60 + atoi(strTime+3);
- endTime = begTime + duration;
- }
- bool Cross (date *x) {
- return x->day == day && x->begTime < endTime && x->endTime > begTime; }
- bool operator() (const date *a, const date *b) {
- return a->day < b->day || a->begTime < b->begTime; }
- } dateObj;
- ostream& operator <<(ostream& out, const date* a)
- {
- out << a->strTime << ' ' << a->duration;
- for(auto& name : a->names)
- out << ' ' << name;
- out << '\n';
- return out;
- }
- class Dates {
- private:
- map <string, vector <date*>> datesMap;
- deque <date> dates;
- public:
- bool Appoint (date &aDate, vector <string> &crossedNames) {
- aDate.SetTime();
- crossedNames.clear();
- for(auto& name : aDate.names)
- for(auto& date : datesMap[name])
- if (aDate.Cross(date))
- crossedNames.push_back(name);
- if (crossedNames.empty()) {
- dates.push_back(aDate);
- for(auto& name : aDate.names) {
- datesMap[name].push_back(&dates.back());
- sort(datesMap[name].begin(), datesMap[name].end(), dateObj);
- }
- }
- return crossedNames.empty();
- }
- bool Find (int aDay, string aName, vector <date*> &result) {
- result.clear();
- for (auto& date : datesMap[aName])
- if (aDay == (date)->day)
- result.push_back(date);
- return !result.empty();
- }
- void Print (ostream& out) {
- for (auto& i : datesMap)
- {
- out << i.first << '\n';
- for (auto& date : i.second)
- {
- out << date;
- }
- }
- }
- };
- int main(int argc, char const *argv[])
- {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- date x;
- string buff;
- vector <string> crossedNames;
- vector <date*> searchResult;
- int print_day;
- string print_name;
- int n; fin >> n;
- Dates dates;
- for (int ii = 0; fin >> buff && ii < n; ++ii)
- {
- if (buff == "APPOINT")
- {
- fin >> x.day >> x.strTime >> x.duration >> x.k;
- x.names.clear();
- for (int i = 0; i < x.k; ++i) {
- fin >> buff;
- x.names.push_back(buff);
- }
- if(dates.Appoint(x, crossedNames)) fout << "OK\n";
- else {
- fout << "FAIL\n";
- for (auto& name : crossedNames)
- fout << name << ' ';
- fout << '\n';
- }
- }
- else if (buff == "PRINT") {
- fin >> print_day >> print_name;
- if(dates.Find(print_day, print_name, searchResult))
- for (auto& date : searchResult)
- fout << date;
- }
- }
- dates.Print(fout);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement