洛谷P1168 中位数

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 using namespace std;
 5 priority_queue<int> big;//大根堆 
 6 priority_queue< int,vector<int>,greater<int> >small;//小根堆
 7 int n,cnt=1,x;
 8 int main()
 9 {
10     scanf("%d",&n);
11     scanf("%d",&x);
12     small.push(x);
13     printf("%d\n",x);
14     while(--n)
15     {
16         scanf("%d",&x);
17         if(x>=small.top()) small.push(x); 
18         else big.push(x);
19         ++cnt;
20         while((int)(small.size()-big.size())>1) big.push(small.top()),small.pop();//size返回类型为unsigned int! 
21         while(big.size()>small.size()) small.push(big.top()),big.pop();
22         if(cnt&1) printf("%d\n",small.top());
23     }
24     return 0;
25 }

 

posted @ 2018-12-22 12:58  宇興  阅读(132)  评论(0编辑  收藏  举报