Dynamic median (on the top of the heap)

Sequentially reading a sequence of integers, each time the number of integers has been read is odd, the output of the median read into the sequence of integers.

Input Format

A first line of input integer P P, the number of data sets representative of the back, followed by a number of lines of each input data set.

The first line of each first input data set representative of an integer number of data sets.

Then enter an integer M M, comprising a data set representative of the number of data, M M always an odd number, the data separated by spaces.

The remaining line data set consists of data sets, each row contains 10 data, the data amount may be less than the last line 10, separated by spaces data.

Output Format

For each data set, the first line output two integers, representing the number of sets of data and the number of output bits (should be added to one-half of a number of data), separated by a space between the data .

The remaining line data set consists of the median output for each row contains 10 data, the data amount may be less than the last line 10, separated by spaces data.

There should be no blank lines in the output.

data range

1P10001≤P≤1000,
1M99991≤M≤9999

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


Ideas: the establishment of two binary heap: a small heap root, a large root heap, with a median of small root stack top of the heap, each time you read a number,
if less than the median, it is inserted into the large root heap, or else to insert a small heap root. If one of the inserted stack element is too large,
then the top of the stack into another pile.

#include <cstdio> 
#include <CString> 
#include <the iostream> 
#include <algorithm> 
#include <the cmath> 
#include <Map>
 the using  namespace STD;
 int A [ 200005 ], B [ 200005 ], C [ 200005 ]; // A small root heap, b stack of large root 
void up ( int n-) // large root stack insertion 
{
     the while (n-> . 1 ) 
    { 
        IF ([n-/ [n-] B> B 2 ]) 
        { 
            the swap (B [ n-], B [n- / 2 ]);
            n=n/2;
        }
        else
           break;
    }
    return;
}
void up1(int n)//小根堆插入
{
    while(n>1)
    {
        if(a[n]<a[n/2])
        {
            swap(a[n],a[n/2]);
            n=n/2;
        }
        else
            break;
    }
    return;
}
void down(intP, int n-) // maintain large root stack delete top of the heap 
{
     int S = P * 2 ;
     the while (S <= n-) 
    { 
        IF (S <n-&& B [S] <B [S + . 1 ]) 
           S ++ ;
         IF ( B [S]> B [P]) 
        { 
            the swap (B [S], B [P]); 
            P = S; 
            S = P * 2 ; 
        } 
        the else 
            BREAK ; 
    } 
    return ; 
} 
void DOWN1 ( int P, int n- )//维护小根堆删除堆顶
{
    int s=p*2;
    while(s<=n)
    {
        if(s<n&&a[s]>a[s+1])
            s++;
        if(a[s]<a[p])
        {
            swap(a[s],a[p]);
            p=s;
            s=p*2;
        }
        else
            break;
    }
    return;
}
int main()
{
    int i,j,m,n,s,w,k1,k2,t,u,v;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d %d",&u,&n);
            for(i=1;i<=n;i++)
                scanf("%d",&c[i]);
            printf("%d %d\n",u,n/2+1);
            k1=k2=0;
            v=0;
            for(i=1;i<=n;i++)
            {
                m=c[i];
                if(i==0)
                    a[++k1]=m;
                else
                {
                    if(m<a[1])
                    {
                        b[++k2]=m;
                        up(k2);
                    }
                    else
                    {
                        a[++k1]=m;
                        up1(k1);
                    }
                }
                if(i%2==1&&k1-1>k2)
                {
                    b[++k2]=a[1];
                    up(k2);
                    a[1]=a[k1--];
                    down1(1,k1);
                }
                else if(i%2==1&&k1-1<k2)
                {
                    a[++k1]=b[1];
                    up1(k1);
                    b[1]=b[k2--];
                    down(1,k2);
                }
                if(i%2==1)
                {
                    printf("%d",a[1]);
                    v++;
                    if(v%10==0)
                        printf("\n");
                    else
                        printf(" ");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zcb123456789/p/11285118.html