Samkit5025

Untitled

Jul 14th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. vector<int> fancyNumbers(int N, vector<int> Q[])
  6. {  
  7.     priority_queue<int> right;
  8.     priority_queue<int,vector<int>,greater<int>> left;
  9.  
  10.     vector<int> res;
  11.  
  12.     for(int i=0;i<N;i++){
  13.         int t = Q[i][0];
  14.         if(t==1){
  15.             int x = Q[i][1];
  16.  
  17.             int temp = left.size() + right.size();
  18.  
  19.             if(temp%3==2){
  20.                 if(x>=right.top()){
  21.                     left.push(x);
  22.                 }
  23.                 else{
  24.                     right.push(x);
  25.                     left.push(right.top());
  26.                     right.pop();
  27.                 }
  28.             }
  29.             else{
  30.                 if(left.size()==0){
  31.                     right.push(x);
  32.                 }
  33.                 else{
  34.                     if(x<=left.top()){
  35.                         right.push(x);
  36.                     }
  37.                     else{
  38.                         left.push(x);
  39.                         right.push(left.top());
  40.                         left.pop();
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         else{
  46.             if(left.size()==0){
  47.                 res.push_back(-1);
  48.             }
  49.             else{
  50.                 res.push_back(left.top());
  51.             }
  52.         }
  53.     }
  54.     return res;
  55. }
  56.  
  57. int main(){
  58.     int N;
  59.     cin>>N;
  60.    
  61.     vector<int> Q[N];
  62.     for(int i=0;i<N;i++){
  63.         int q;
  64.         cin>>q;
  65.         Q[i].push_back(q);
  66.         if(q==1){
  67.             int x;
  68.             cin>>x;
  69.             Q[i].push_back(x);
  70.         }
  71.     }
  72.  
  73.     vector<int> res = fancyNumbers(N,Q);
  74.     for(auto i : res){
  75.         cout<<i<<endl;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment