码迷,mamicode.com
首页 > 其他好文 > 详细

Black Box

时间:2019-08-27 22:52:01      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:amp   style   pre   put   第一个   step   open   queue   mes   

目前还在评测,因为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

 

Black Box

标签:amp   style   pre   put   第一个   step   open   queue   mes   

原文地址:https://www.cnblogs.com/coder-cjh/p/11421123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!