Advertisement
ostapdontstop

dates

Sep 10th, 2019
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <deque>
  5. #include <map>
  6. #include <string>
  7. #include <algorithm>
  8. #include <cstdlib>
  9. using namespace std;
  10.  
  11.  
  12. struct date {
  13.     int day, duration, k, begTime = 0, endTime = 0;
  14.     char strTime[6];
  15.     vector <string> names;
  16.  
  17.     void SetTime() {
  18.         begTime = atoi(strTime)*60 + atoi(strTime+3);
  19.         endTime = begTime + duration;
  20.     }
  21.  
  22.     bool Cross (date *x) {
  23.         return x->day == day && x->begTime < endTime && x->endTime > begTime; }
  24.  
  25.     bool operator() (const date *a, const date *b) {
  26.         return a->day < b->day || a->begTime < b->begTime; }
  27.  
  28. } dateObj;
  29.  
  30. ostream& operator <<(ostream& out, const date* a)
  31. {
  32.     out << a->strTime << ' ' << a->duration;
  33.  
  34.     for(auto& name : a->names)
  35.         out << ' ' << name;
  36.  
  37.     out << '\n';
  38.  
  39.     return out;
  40. }
  41.  
  42.  
  43.  
  44. class Dates {
  45. private:
  46.     map <string, vector <date*>> datesMap;
  47.     deque <date> dates;
  48.  
  49. public:
  50.  
  51.  
  52.     bool Appoint (date &aDate, vector <string> &crossedNames) {
  53.  
  54.         aDate.SetTime();
  55.         crossedNames.clear();
  56.  
  57.         for(auto& name : aDate.names)
  58.             for(auto& date : datesMap[name])
  59.                 if (aDate.Cross(date))
  60.                     crossedNames.push_back(name);
  61.  
  62.  
  63.         if (crossedNames.empty()) {
  64.  
  65.             dates.push_back(aDate);
  66.  
  67.             for(auto& name : aDate.names) {
  68.                 datesMap[name].push_back(&dates.back());
  69.                 sort(datesMap[name].begin(), datesMap[name].end(), dateObj);
  70.             }
  71.  
  72.         }
  73.  
  74.         return crossedNames.empty();
  75.     }
  76.  
  77.  
  78.     bool Find (int aDay, string aName, vector <date*> &result) {
  79.  
  80.         result.clear();
  81.  
  82.         for (auto& date : datesMap[aName])
  83.             if (aDay == (date)->day)
  84.                 result.push_back(date);
  85.  
  86.         return !result.empty();
  87.     }
  88.  
  89.  
  90.     void Print (ostream& out) {
  91.         for (auto& i : datesMap)
  92.         {
  93.             out << i.first << '\n';
  94.  
  95.             for (auto& date : i.second)
  96.             {
  97.                 out << date;
  98.             }
  99.  
  100.         }
  101.     }
  102.  
  103.  
  104. };
  105.  
  106. int main(int argc, char const *argv[])
  107. {
  108.     ifstream fin("input.txt");
  109.     ofstream fout("output.txt");
  110.  
  111.     date x;
  112.     string buff;
  113.  
  114.     vector <string> crossedNames;
  115.     vector <date*> searchResult;
  116.  
  117.     int print_day;
  118.     string print_name;
  119.  
  120.     int n; fin >> n;
  121.  
  122.     Dates dates;
  123.  
  124.     for (int ii = 0; fin >> buff && ii < n; ++ii)
  125.     {
  126.  
  127.         if (buff == "APPOINT")
  128.         {
  129.             fin >> x.day >> x.strTime >> x.duration >> x.k;
  130.             x.names.clear();
  131.             for (int i = 0; i < x.k; ++i) {
  132.                 fin >> buff;
  133.                 x.names.push_back(buff);
  134.             }
  135.  
  136.             if(dates.Appoint(x, crossedNames)) fout << "OK\n";
  137.  
  138.             else {
  139.                 fout << "FAIL\n";
  140.                 for (auto& name : crossedNames)
  141.                     fout << name << ' ';
  142.                 fout << '\n';
  143.             }
  144.  
  145.         }
  146.  
  147.         else if (buff == "PRINT") {
  148.  
  149.             fin >> print_day >> print_name;
  150.  
  151.             if(dates.Find(print_day, print_name, searchResult))
  152.                 for (auto& date : searchResult)
  153.                     fout << date;
  154.                
  155.         }
  156.     }
  157.  
  158.     dates.Print(fout);
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement