pat Grade A 1057 Online query/dynamic query, space for time

Querying the M-th smallest number in the unordered array, inserting and deleting continuously during the query, the cost of sorting each time the query is too large.
Take the maximum possible value maxn as the range, divide the numbers into ceil(sqrt(maxn)) groups, the number of floor(sqrt(maxn)) in each group, and set up two arrays, one counts the number of times each number appears, the other Count the number of numbers in each group.
When querying, first determine which block it is in, and then find the Mth block in that block.

#include <cstdio>
#include <stack>
#include <cstring>

using namespace std;

const int maxn = 100001;
const int blk = 317;
const int blk_size = 316;
int N;

stack<int> s;
int num_count[maxn]={
    
    }; //每个数的个数
int blk_count[blk]={
    
    }; //每个块中数的个数

int findMedian();

int main(){
    
    
    scanf("%d", &N);
    while(N--){
    
    
        char command[15];
        scanf("%s", command);
        if(strcmp(command, "Push")==0){
    
    
            int v;
            scanf("%d", &v);
            s.push(v);
            num_count[v]++;
            blk_count[v/blk_size]++;
        }
        else if(strcmp(command, "Pop")==0){
    
    
            if(s.empty()) printf("Invalid\n");
            else{
    
    
                int v = s.top();
                printf("%d\n", v);
                s.pop();
                num_count[v]--;
                blk_count[v/blk_size]--;
            }
        }
        else{
    
    
            if(s.empty()) printf("Invalid\n");
            else printf("%d\n", findMedian());
        }
    }

    return 0;
}

int findMedian(){
    
    
    int n = s.size();
    int M = (n%2==0)?(n/2):((n+1)/2);
    int sum = 0;
    for(int b=0; b<blk; b++){
    
    
        if(sum+blk_count[b]<M){
    
    
            sum += blk_count[b];
            continue;
        }
        else{
    
    
            for(int i=b*blk_size; i<(b+1)*blk_size; i++){
    
    
                if(sum+num_count[i]<M){
    
    
                    sum += num_count[i];
                    continue;
                }
                else return i;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/sinat_37517996/article/details/104677272