# 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


 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】 Running Median

[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆中 每次插入元素时,先将插入元素与小根堆堆顶比较,如果比堆顶小,则插入小根堆,否则,插入大根堆,然后,判断两个堆 的元素个数是否平衡,若不平衡,则交换两个堆的堆顶 [代码] #include <algorithm> #include <bitset> #include <ccty

## [ACM] POJ 2442 Sequence (堆的性质）

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7011   Accepted: 2262 Description Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's