Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. struct event {
  9.     int type; // 1 - прибытие 0 -отправление
  10.     int time_1;
  11.     int time_2;
  12.     int station;
  13.  
  14.     event(int type, int time_1, int time_2, int station) : type(type), time_1(time_1), time_2(time_2), station(station) {}
  15.  
  16.  
  17. };
  18. bool operator <(event const& a, event const& other) {
  19.     return a.time_1 < other.time_1 || (a.time_1 == other.time_1 && a.type > other.type);
  20. }
  21.  
  22. int main()
  23. {
  24.     int t;
  25.     cin >> t;
  26.  
  27.     int n;
  28.     cin >> n;
  29.  
  30.     set<event> arr;
  31.     vector<int> train1;
  32.     vector<int> train2;
  33.  
  34.  
  35.  
  36.     for (int i = 0; i < n; i++) {
  37.         int a, b;
  38.         cin >> a >> b;
  39.         arr.insert(event(0, a, b, 1));
  40.     }
  41.  
  42.     int m;
  43.     cin >> m;
  44.  
  45.     for (int i = 0; i < m; i++) {
  46.         int a, b;
  47.         cin >> a >> b;
  48.         arr.insert(event(0, a, b, 2));
  49.     }
  50.  
  51.     while (!arr.empty()) {
  52.         event cur = *arr.begin();
  53.         arr.erase(arr.begin());
  54.  
  55.         if (cur.type == 0) {
  56.             if (cur.station == 1) {
  57.                 if ((int)train1.size() > 0) {
  58.                     arr.insert(event(1, cur.time_2 + t, -1, 2));
  59.                     train1.pop_back();
  60.                 }
  61.                 else {
  62.                     train1.push_back(0);
  63.                     arr.insert(event(1, cur.time_2 + t, -1, 2));
  64.                     train1.erase(train1.begin());
  65.                 }
  66.             }
  67.             else {
  68.                 if ((int)train2.size() > 0) {
  69.                     arr.insert(event(1, cur.time_2 + t, -1, 1));
  70.                     train2.pop_back();
  71.                 }
  72.                 else {
  73.  
  74.                     arr.insert(event(1, cur.time_2 + t, -1, 1));
  75.                 }
  76.             }
  77.  
  78.         }
  79.         else {
  80.             if (cur.station == 1) {
  81.                 train1.push_back(0);
  82.             }
  83.             else {
  84.                 train2.push_back(0);
  85.             }
  86.         }
  87.     }
  88.  
  89.     int res = train1.size() + train2.size();
  90.  
  91.     cout << res << endl;
  92.  
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement