D_L3

Рожден ден

Jan 26th, 2024
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. struct Guest{
  11.     int comes;
  12.     int goes;
  13.     int index;
  14. };
  15.  
  16. struct comeComparator{
  17.     bool operator()(Guest g1, Guest g2) const{
  18.         return g1.comes > g2.comes;
  19.     }
  20. };
  21. struct goComparator{
  22.     bool operator()(Guest g1, Guest g2) const{
  23.         return g1.goes > g2.goes;
  24.     }
  25. };
  26.  
  27.  
  28.  
  29. int main() {
  30.     int n, a, b, t;
  31.     priority_queue<Guest, vector<Guest>, comeComparator> expect;
  32.     priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> there; //goes, chair
  33.     priority_queue<int, vector<int>, greater<int>> chairs;
  34.    
  35.     cin >> n;
  36.     for(int i = 0; i < n; i++){
  37.         cin >> a >> b;
  38.         expect.push({a, b, i});
  39.         chairs.push(i);
  40.     }
  41.     int currTime = 0;
  42.     cin >> t;
  43.    
  44.     while(!expect.empty()){
  45.         while(expect.top().comes <= currTime){
  46.             if(expect.top().index == t){
  47.                 cout << chairs.top();
  48.                 return 0;
  49.             }
  50.             there.push({expect.top().goes, chairs.top()});
  51.            
  52.             chairs.pop();
  53.             expect.pop();
  54.         }
  55.         while(!there.empty() && there.top().first <= currTime){
  56.             chairs.push(there.top().second);
  57.             there.pop();
  58.         }
  59.         if(!expect.empty()){
  60.             currTime = expect.top().comes;
  61.             if(!there.empty())
  62.                 currTime = min(currTime, there.top().first);
  63.         }
  64.     }
  65.    
  66.    
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment