-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpoj_RMID2.cpp
89 lines (84 loc) · 2 KB
/
Spoj_RMID2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
maneesh(maik)
*/
// Two Priority queues
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<bitset>
#include<deque>
#include<iterator>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define lb lower_bound
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false)
int main()
{
ll tc;
scanf("%lld", &tc);
while(tc--)
{
priority_queue<ll>pq_max;
priority_queue<ll, vector<ll>, greater<ll>>pq_min;
ll n;
while(true)
{
scanf("%lld", &n);
if(n==0)
{
while(pq_min.size())
pq_min.pop();
while(pq_max.size())
pq_max.pop();
break;
}
if(n==-1)
{
printf("%lld\n",pq_max.top());
pq_max.pop();
while(pq_max.size()<pq_min.size())
{
ll x=pq_min.top();
pq_min.pop();
pq_max.push(x);
}
}
else
{
pq_min.push(n);
if(pq_min.size()>0 && pq_max.size()>0)
{
if(pq_min.top() < pq_max.top())
{
ll x=pq_min.top();
ll y=pq_max.top();
pq_min.pop();
pq_max.pop();
pq_min.push(y);
pq_max.push(x);
}
}
while(pq_max.size() < pq_min.size())
{
ll x=pq_min.top();
pq_min.pop();
pq_max.push(x);
}
}
}
printf("\n");
}
return 0;
}