Black Box

目前还在评测,因为UVA评测姬它了,但是udebug是可过的

Step 1

首先我们可以想到全部sort一遍,但是这样效率太低了,我们可以想到另一种方法,因为前I个数是上一次询问就已经保存下来的,我们只要找到这第i+1个数就好了

Step 2

确定数据结构:对顶堆

第一个用来保存前i个数,第二个的堆顶就是第i+1个数

Step 3:

#include<bits/stdc++.h>
using namespace std;
priority_queue<int> q1;
priority_queue<int,vector<int>,greater<int> > q2;
int t,n,m;
int a[500005];
int main(){
    scanf("%d",&t);
    while(t--){
        int l=1,r;
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=m;i++){
            scanf("%d",&r);
            for(int j=l;j<=r;j++){//上次的端点到这一次的端点是要处理的区间
                q1.push(a[j]);
                if(q1.size()==i)q2.push(q1.top()),q1.pop();//如果已经超限了,就放到第二个堆里
            }
            l=r+1;
            printf("%d\n",q2.top());
            q1.push(q2.top()),q2.pop();//把答案也存进去
        }
        while(!q1.empty())q1.pop();
        while(!q2.empty())q2.pop();//多测不清空,爆零两行泪
        if(t)puts("");
    }
} 
Code

 

posted @ 2019-08-27 22:03  Coder_cjh  阅读(173)  评论(0编辑  收藏  举报