POJ 3784 Running Median

POJ 3784 Running Median

POJ传送门

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

Source

Greater New York Regional 2009

题解:

正解:对顶堆。

如果没学过对顶堆,请点击这里

这道题要动态维护一个中位数。也就是输入到一个数的时候,求当前序列的中位数。那么我们通过对顶堆的思想,就可以想到,维护两个序列,一个是大根堆,一个是小根堆,它们的堆顶表示当前状态下的最大值和最小值。

什么是中位数?中位数就是排好序之后最中间的那个数。无论是大根堆还是小根堆,都能维护好一个有序序列。那么我们只需要选择当前状态下最中间的数就好了。

所以这道题我们应该用对顶堆。

大根堆维护这个序列中最大的元素,小根堆维护这个序列中最小的元素,我们求中位数的时候,利用了“用大的换小的”这个思想,队列中的大数我们用小根堆中的最小数来交换,那么无论是大根堆和小根堆,我们都删除了一个最大/最小元素,而交换过来的东西肯定不会在堆顶,这样就相当于在一个序列的基础上,“一层层地砍”最大/最小值。等到我们的最大值和最小值相等的时候,就相当于砍掉了中位数之前所有的最大/最小值。而最后剩下的这个堆顶就是我们要找的答案。

也就是说,只要两个堆的堆顶元素不等,我们就交换,这样当退出循环的时候,我们两个堆的堆顶元素就是我们要找的当前状态下的中位数,输出哪个堆的堆顶都是可以的,随你心情。

细节实现要注意每组数据之前的清零工作以及每逢10个就换行的判断。

至于小根堆,还是两种方法,一种是取负大根堆,一种是重新声明小根堆。

我两份代码都打了一遍:

代码1:

#include<cstdio>
#include<queue>
using namespace std;
int id,m;
priority_queue<int> bq,sq;
int main()
{
    
    int p;
    scanf("%d",&p);
    while(p--)
    {
        while(!bq.empty())
            bq.pop();
        while(!sq.empty())
            sq.pop();
        scanf("%d%d",&id,&m);
        printf("%d %d
",id,m+1>>1);
        for(int i=1;i<=m;i++)
        {
            int x;
            scanf("%d",&x);
            bq.push(x);sq.push(-x);
            if(i%2==0)
                continue;
            while(bq.top()!=-sq.top())
            {
                int a=bq.top();bq.pop();
                int b=-sq.top();sq.pop();
                bq.push(b);sq.push(-a);
            }
            printf("%d ",bq.top());
            if(((i+1)/2)%10==0 || i==m)
                printf("
");
        }
    }
    return 0;
}

代码2:

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
int id,m;
priority_queue<int> bq;
priority_queue<int,vector<int>,greater<int> > sq;
int main()
{
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
    int p;
    scanf("%d",&p);
    while(p--)
    {
        while(!bq.empty())
            bq.pop();
        while(!sq.empty())
            sq.pop();
        scanf("%d%d",&id,&m);
        printf("%d %d
",id,m+1>>1);
        for(int i=1;i<=m;i++)
        {
            int x;
            scanf("%d",&x);
            bq.push(x);sq.push(x);
            if(i%2==0)
                continue;
            while(bq.top()!=sq.top())
            {
                int a=bq.top();bq.pop();
                int b=sq.top();sq.pop();
                bq.push(b);sq.push(a);
            }
            printf("%d ",bq.top());
            if(((i+1)/2)%10==0 || i==m)
                printf("
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/fusiwei/p/11436944.html