1. 程式人生 > >Running Median POJ - 3784 (對頂堆/優先隊列)

Running Median POJ - 3784 (對頂堆/優先隊列)

數量 ger per median may nta -- closed bit

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

題意:每組M個數,然後對於每組數讀入的時候,只要讀入了奇數個的數,就求出先前讀入數的中位數,然後輸出

思路:可以采用兩個優先隊列的做法,如果當前讀入的數>當前中位數,插入小根堆,否則插入大根堆,這樣實際上就維護了中位數相鄰兩側的值。
然後維護 num【小根堆】 - num【大根堆】 <= 1,就是維護中位數兩側的數量應當均分,這樣小根堆的top,即是中位數

技術分享圖片
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<queue>
 4 using namespace std;
 5 
 6 int t;
 7 int cas,n;
 8 const int maxn = 1e4+5;
 9 int ans[maxn];
10 int main()
11 {
12     scanf("%d",&t);
13     while(t--)
14     {
15 priority_queue<int,vector<int>,greater<int> >que1;
16 priority_queue<int,vector<int>,less<int> >que2;
17 
18         scanf("%d%d",&cas,&n);
19         printf("%d %d\n",cas,n/2+1);
20         int tmp;
21         int l=0,r=0,num=0;
22         int cnt = 0;
23         for(int i=1;i<=n;i++)
24         {
25             scanf("%d",&tmp);
26             if(tmp > num)
27             {
28                 que1.push(tmp);
29                 r++;
30             }
31             else
32             {
33                 que2.push(tmp);
34                 l++;
35             }
36             if(r < l)
37             {
38                 int f = que2.top();
39                 que2.pop();
40                 que1.push(f);
41                 r++,l--;
42             }
43             else if(r > l + 1)
44             {
45                 r--,l++;
46                 int f = que1.top();
47                 que1.pop();
48                 que2.push(f);
49             }
50             num = que1.top();
51             if(i&1)
52             {
53                 ans[++cnt] = num;
54             }
55         }
56         for(int i=1;i<=cnt;i++)
57         {
58             printf("%d",ans[i]);
59             if(i != cnt && i%10!=0)printf(" ");
60             else printf("\n");
61         }
62     }
63 }
View Code



Running Median POJ - 3784 (對頂堆/優先隊列)