http://poj.org/problem?id=1442

题意 : 题目中对给出的数字有两种操作ADD(I)操作,将ADD括号里的数字 I 加到数列里边去,然后是自动排好序的,每一个数列前边都会有一个数字ss,代表着前边GET的数量有ss个,GET就是将现有数列显现出来,然后给你N个依次要添加的数字,下边一行是M个数字,代表着每个get前边有的add的数量,然后输出这个序列中第ss个数。

思路 : 因为要排序,所以就直接用优先队列就好,但是用一个不行,会超时,至于为什么,其实我也不知道,就用了两个,大小根堆一起用,把小根堆里要输出的那个数前边的数字放到大根堆里,循环操作,如果小根堆首元素小于大根堆首元素,就交换这俩元素

/*7 4
3 1 -4 2 8 -1000 2
1 2 6 6
Sample Output
3 3 1 2*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
using namespace std ;
const int maxn = 30100 ;
int main()
{
    int m,n ;
    priority_queue<int ,vector<int> ,less<int> >Q;//大根堆
    priority_queue<int ,vector<int> ,greater<int> >QQ;//小根堆
    scanf("%d %d",&m,&n);
    int a[maxn] ;
    for(int i = 1 ; i <= m ; i++)
        scanf("%d",&a[i]) ;
        int be = 1 ,c,b;
    for(int i = 1 ; i <= n ; i++)
    {
        scanf("%d",&b) ;
        for(int j = be ; j <= b ; j++)
        {
            QQ.push(a[j]) ;
            if(!Q.empty()&&Q.top() > QQ.top())
            {
                c = Q.top() ;
                Q.pop() ;
                Q.push(QQ.top());
                QQ.pop() ;
                QQ.push(c) ;
            }
        }
        be = b+1 ;
        printf("%d\n",QQ.top()) ;
        Q.push(QQ.top());
        QQ.pop() ;
    }
    return 0 ;
}
View Code

 

posted on 2013-08-22 11:11  枫、  阅读(578)  评论(0编辑  收藏  举报