poj 3784(对顶堆)

Posted hymscoty

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 3784(对顶堆)相关的知识,希望对你有一定的参考价值。

Running Median
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1824   Accepted: 889

Description

For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed. The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values.

Output

For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output.

Sample Input

3 
1 9 
1 2 3 4 5 6 7 8 9 
2 9 
9 8 7 6 5 4 3 2 1 
3 23 
23 41 13 22 -3 24 -31 -11 -8 -7 
3 5 103 211 -311 -45 -67 -73 -81 -99 
-33 24 56

Sample Output

1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3 
-7 -3


题意
给n个数,第奇数次输入的时候,输出当前数中的中位数。


思路
对顶堆。建立一个大根堆和小根堆,如果当前元素大于小根堆堆顶元素则放入小根堆,否则放入大根堆。
这样保证小根堆的任意元素>大根堆的任意元素,并且mnq.size==mxq.size+1即小根堆比大根堆多1个元素(小根堆堆顶元素:中位数)
如果不满足上面的等式,则对两个堆进行调整。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <functional>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 priority_queue<int> mxq;
10 priority_queue<int,vector<int>,greater<int> > mnq;
11 
12 vector<int> res;
13 
14 void add(int x){
15     if(mnq.empty()){
16         mnq.push(x);
17         return;
18     }
19     if(x>mnq.top()) mnq.push(x);
20     else mxq.push(x);
21     while(mnq.size()<mxq.size()){
22         mnq.push(mxq.top());
23         mxq.pop();
24     }
25     while(mnq.size()>mxq.size()+1){
26         mxq.push(mnq.top());
27         mnq.pop();
28     }
29 }
30 
31 int main(){
32     int T;
33     scanf("%d",&T);
34     while(T--){
35         while(!mnq.empty()) mnq.pop();
36         while(!mxq.empty()) mxq.pop();
37         res.clear();
38         int n,m;
39         scanf("%d %d",&n,&m);
40         for(int i=1;i<=m;i++){
41             int x;scanf("%d",&x);
42             add(x);
43             if(i%2) res.push_back(mnq.top());
44         }
45         printf("%d %d\n",n,res.size());
46         for(int i=0;i<res.size();i++){
47             if(i%10==0&&i) putchar(\n);
48             if(i%10>=1) putchar( );
49             printf("%d",res[i]);
50         }
51         printf("\n");
52     }
53     return 0;
54 }

 


以上是关于poj 3784(对顶堆)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3784 Running Median

Running Median POJ - 3784

$Poj3784 Running Median$

POJ 3784. Running Median

对顶堆与应用

黑匣子 对顶堆