AcWing:. 106 Dynamic median (top of the stack)

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

 

Algorithm: on top of the heap

Solution: This problem is to find the median dynamic, that is not the output number n, n odd representatives, which outputs the median. Heap need to use this data structure, you need to create two stacks, a heap is small to large, is a digit from the beginning to the end of the range; a heap is descending, that is, from the beginning to the median of a former range. With these two complementary pile each other's deficiencies, and then come to different result each time.

 

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <functional>

using namespace std;

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int cas, n;
        int cnt = 0;
        scanf("%d %d", &cas, &n);
        printf("%d %d\n", cas, (n + 1) / 2 ); 
        The priority_queue < int , Vector < int >, Greater < int >> Ql;      // rootlets reactor, from small to large, a rear half 
        The priority_queue < int > Q2;      // large root stack, in descending , represents the first half 
        for ( int I = . 1 ; I <= n-; I ++ ) {
             int X; 
            Scanf ( " % D " , & X);
             IF (q1.empty ()) { 
                q1.push (X); 
            } the else {
                 IF (X> q1.top()) {
                    q1.push(x);
                } else {
                    q2.push(x);
                }
                if(q1.size() < q2.size()) {
                    q1.push(q2.top());
                    q2.pop();
                }
                if(q1.size() > q2.size() + 1) {
                    q2.push(q1.top());
                    q1.pop();
                }
            }
            if(i & 1) {
                cnt++;
                if(cnt % 10 == 0) {
                    printf("%d\n", q1.top());
                } else {
                    printf("%d ", q1.top());
                }
            }
        }
        if(cnt % 10 != 0) {
            printf("\n");
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/buhuiflydepig/p/11295240.html