POJ 3784-Running Median[链表]

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
.
.
.
.
.

分析

将所有的数列都读入后排序,最后一个中位数显而易见
然后链表连一发 再按照输入的顺序 从后往前 两个两个删除
跟对顶堆的做法相似
当删除的两个数分别大于和小于当前的中位数 直接记录
当删除的两个数都大于等于当前的中位数 记录last
当删除的两个数都小于等于当前的中位数 记录next
.
.
.
.
.

程序:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int b[20000],ans[20000],v[20000];

struct node
{
    int last,next,c,p;
}a[200000];

bool cmp(node x,node y)
{
    if (x.c==y.c) return x.p<y.p;else return x.c<y.c;
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n,p,ss=0,sl=0,sr=0,s,sx;
        scanf("%d%d",&p,&n);
        printf("%d %d
",p,(n+1)/2);
        for (int i=1;i<=n;i++) 
        {
            scanf("%d",&b[i]);
            a[i].c=b[i];
            a[i].p=i;       
        }
        sort(a+1,a+1+n,cmp);
        for (int i=1;i<=n;i++)
        {
            a[i].last=i-1;
            a[i].next=i+1;
            v[a[i].p]=i;
        } 
        s=a[n/2+1].c;
        sx=n/2+1;
        if (n%2==0) ans[++ss]=(a[n/2].c+a[n/2+1].c)/2; else ans[++ss]=a[n/2+1].c;
        for (int i=n;i>1;i-=2)
        {
            if (v[i]<sx&&v[i-1]>sx||v[i]>sx&&v[i-1]<sx)
            {
                ans[++ss]=a[sx].c;
            } else 
            if (v[i]<=sx&&v[i-1]<=sx)
            {
                sx=a[sx].next;
                ans[++ss]=a[sx].c;
            } else
            {
                sx=a[sx].last;
                ans[++ss]=a[sx].c;
            }
            int x=a[v[i]].last,y=a[v[i]].next;
            a[y].last=x;
            a[x].next=y;
            x=a[v[i-1]].last;
            y=a[v[i-1]].next;
            a[y].last=x;
            a[x].next=y;
        }
        int z=0;
        for (int i=ss;i>0;i--) 
        {
            if (z!=0) printf(" ");
            z++;
            printf("%d",ans[i]);
            if (z==10) 
            {
                printf("
");
                z=0;
            }
        }
        if (z!=0) printf("
");
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/YYC-0304/p/9499894.html