Description
For the problem, you'll write a program, the reads in a sequence of 32-bit signed integers. After all 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 are the number of data sets that follow. The first line of all 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, and separated by a single space. The last line of the dataset may contain less than 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 be is on the following lines, and the per line separated by a single space. The last line is than elements, but at least 1 element. There should is 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-3 3 24 56
Sample Output
1 51 2 3 4 52 59 8 7 6 53 1223 23 22 22 13 3 5 5 3-3-7-3
Title: For the number of n, each input to an odd number of numbers, you will find the median.
Problem-solving ideas: I started on the direct violence, to the odd digits, sort, find the median, so that there is no time-out on the uvalive, but on the POJ is timed out, looked at the online code to know that processing such a dynamic median use heap to maintain, and this heap using the priority queue to simulate, See priority queue for the second time in a week. Maintain a large heap and a small Gan, and ensure that all the number in the large root heap is smaller than all the number in the small root heap, and the size of Dagen is equal to the small Gan or 1, the top of the heap heap is the median. For the newly inserted number, the comparison with the median determines which heap is inserted, and the size of the two heaps is maintained after the insertion. Each maintenance requires an operation on a constant heap, so the complexity O (NLOGN).
1#include <cstdio>2#include <cstring>3#include <queue>4 using namespacestd;5priority_queue<int,vector<int>,greater<int> > BH;///from small to large6priority_queue<int,vector<int>,less<int> > sh;///from big to small7 intMain ()8 {9 intT,n,num;Ten inti,j,m,p,q,x,y,z,k; Onescanf"%d",&t); A while(t--) - { -scanf"%d%d",&num,&n); theprintf"%d%d\n", num,n/2+1); - while(!bh.empty ()) - { - Bh.pop (); + } - while(!sh.empty ()) + { A Sh.pop (); at } - for(i=1; i<=n; i++) - { -scanf"%d",&x); - if(Sh.empty () | | Sh.top () >x) - { inSh.push (x);///put in a small heap - } to Else + { -Bh.push (x);///put in a large pile the } * if(Sh.size () > (i+1)/2) $ {Panax Notoginsengx=sh.top (); - Sh.pop (); the Bh.push (x); + } A if(Sh.size () < (i+1)/2) the { +x=bh.top (); - Bh.pop (); $ Sh.push (x); $ } - if(i%2) - { theprintf"%d", Sh.top ()); - if(i==n| | (i+1)% -==0)Wuyi { theprintf"\ n"); - } Wu Else - { Aboutprintf" "); $ } - } - } - } A return 0; +}
POJ 3784 Running Median (median of dynamic maintenance)