poj_1442_Black Box_优先队列

来源:互联网 发布:这个驸马有淘宝gl下载 编辑:程序博客网 时间:2024/05/17 00:18

题目大意

有两个数列,一个为add,一个为get,每次从add里取一个数加入数列,然后每到数列的长度在get中时,输出数列中前x个数中的最小值,没get,x加一

思路

开两个队列,一个升序,一个降序,每次往里面插入数,并进行是否大于x的判断,如果到了get中的数时,输出堆顶并累加x
O(2*m*logm)

#include <stdio.h>#include <queue>int a[100000],b[100000];using namespace std;priority_queue<int> heap;priority_queue<int,vector<int>,greater<int> > heap1;int main(){    int n,m;    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",&b[i]);    int i=0,j=1,k=1;    while (j<=m)    {        if (i==b[j])        {            j++;            if (heap.size()<k)            {                heap.push(heap1.top());                heap1.pop();            }            printf("%d\n",heap.top());            k++;        }        else        {            i++;            if (heap.size()<k)            {                heap1.push(a[i]);                heap.push(heap1.top());                heap1.pop();            }            else if (heap.top()>a[i])               {                heap1.push(heap.top());                heap.pop();                heap.push(a[i]);            }            else heap1.push(a[i]);        }    }    return 0;}
1 0
原创粉丝点击