-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedianinstream.cpp
60 lines (60 loc) · 1.11 KB
/
Medianinstream.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
#include <bits/stdc++.h>
using namespace std;
int median(int i,int &m, priority_queue <int>&max, priority_queue<int, vector<int>, greater<int> >&min)
{
if (max.size()==min.size())
{
if (i<m)
{
max.push(i);
m=max.top();
}
else
{
min.push(i);
m=min.top();
}
}
if (max.size()-min.size()==1)
{
if(i<m)
{
min.push(max.top());
max.pop();
max.push(i);
}
else
{
min.push(i);
}
m=(max.top()+min.top())/2;
}
if (max.size()-min.size()==-1)
{
if (i>m)
{
max.push(min.top());
min.pop();
min.push(i);
}
else
{
max.push(i);
}
m=(max.top()+min.top())/2;
}
return m;
}
int main()
{
int i,n,a,m=0;
cin>>n;
priority_queue <int> max;
priority_queue <int,vector<int>,greater<int> > min;
for (i=0;i<n;i++)
{
cin>>a;
cout<<median(a,m,max,min)<<endl;
}
return 0;
}