poj1442--Black Box--优先队列维护堆&&multiset维护堆

Black Box
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6002 Accepted: 2420

Description

Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions: 

ADD (x): put element x into Black Box; 
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending. 

Let us examine a possible sequence of 11 transactions: 

Example 1 
N Transaction i Black Box contents after transaction Answer 

      (elements are arranged by non-descending)   

1 ADD(3)      0 3   

2 GET         1 3                                    3 

3 ADD(1)      1 1, 3   

4 GET         2 1, 3                                 3 

5 ADD(-4)     2 -4, 1, 3   

6 ADD(2)      2 -4, 1, 2, 3   

7 ADD(8)      2 -4, 1, 2, 3, 8   

8 ADD(-1000)  2 -1000, -4, 1, 2, 3, 8   

9 GET         3 -1000, -4, 1, 2, 3, 8                1 

10 GET        4 -1000, -4, 1, 2, 3, 8                2 

11 ADD(2)     4 -1000, -4, 1, 2, 2, 3, 8   

It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type. 


Let us describe the sequence of transactions by two integer arrays: 


1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2). 

2. u(1), u(2), ..., u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, ... and N-transaction GET. For the Example we have u=(1, 2, 6, 6). 

The Black Box algorithm supposes that natural number sequence u(1), u(2), ..., u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), ..., A(u(p)) sequence. 


Input

Input contains (in given order): M, N, A(1), A(2), ..., A(M), u(1), u(2), ..., u(N). All numbers are divided by spaces and (or) carriage return characters.

Output

Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.

Sample Input

7 4
3 1 -4 2 8 -1000 2
1 2 6 6

Sample Output

3
3
1
2

Source


这道题学到两个东西:

1.怎样通过结构体定义使用priority_queue

2.怎样在结构体里面重载<运算符从而省去mycompare函数

3.Priority_queue确实比multiset常优,但是写起来烦。

4.C++中有太多东西是左闭右开的了,比如mulitiset  a中的a.end(),返回的并不是a中最后一个元素的指针。



题目有点长,题意还好。。。不算抽。

题意就是,给你N个查询ai,第i个查询加入ai个数后,已排好序的ai个数中第i个数的大小。

做法就是,用两个堆来维护,一个最小堆,一个最大堆。

在最大堆中放i-1个数,在最小堆中放第i个到第N个元素,

维护一点,最小堆的顶部(最小元素)大于最大堆顶部(最大元素),

那么最小堆的顶部即为所求的答案。

首先用单调队列写了一个。

以下是代码+注释,

12105497lovellp1442Accepted672K157MSC++1394B2013-09-13 10:54:18

#include <iostream>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxnum=3e4+100;
struct max_heap
{
    int x;
    bool operator<(const max_heap&a) const //第一次写重载运算符,感觉还蛮好ORZ,可以少写一个my_compare函数了
    {
        return x<a.x;
    }
};

struct min_heap
{
    int x;
    bool operator<(const min_heap&a) const //这个运算符是相反定义的,因而通过单调队列它维护了一个最小堆。
    {
        return x>a.x;
    }
};

int a[maxnum];
priority_queue<max_heap> max_h;
priority_queue<min_heap> min_h;
max_heap max_t;//这个东东神奇的功效接下来就会看到
min_heap min_t;//这个东东神奇的功效接下来就会看到

int main()
{
    //freopen("input.txt","r",stdin);
    int m,n;
    cin>>m>>n;
    for(int i=1;i<=m;++i)
        scanf("%d",&a[i]);//先读入所有数据,再慢慢进行处理
    int num=1;
    for(int i=1;i<=n;++i)
    {
        int t;
        scanf("%d",&t);
        while(num<=t) //这里很精妙,只有当所给查询位置大于已加入元素个数的时候才进行添加
        {
            if(!max_h.empty()&&max_h.top().x>a[num])//只有当最大堆非空才进行比较!这点很重要!
            {
                int temp=max_h.top().x;
                max_h.pop();
                max_t.x=a[num];
                max_h.push(max_t);
                min_t.x=temp;
                min_h.push(min_t);
            }
            else
            {
                min_t.x=a[num];
                min_h.push(min_t);
            }
            ++num;
        }
        printf("%d\n",min_h.top().x);//输出
        //接下来更是精妙的所在,即每过一个查询,都要维护最大堆的个数+1!
        int temp=min_h.top().x;
        max_t.x=temp;
        min_h.pop();
        max_h.push(max_t);
    }
    return 0;
}

后来想想multiset好像也能实现这个功能,又用multiset写了一个,

即用Multiset维护两个堆,

大体思想不变,

但是如学长所说~~~Multiset作为非线性结构,的确比priotity_queue慢了常数级~~~


12106171lovellp1442Accepted1708K688MSC++1179B2013-09-13 15:15:40

以下是代码+注释:
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
const int maxnum=3e4+100;

int hehe[maxnum];

multiset<int> a;
multiset<int> b;
int main()
{
    //freopen("input.txt","r",stdin);
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;++i)
        scanf("%d",&hehe[i]);
    int num=1;
    for(int i=1;i<=q;++i)
    {
        int t;
        scanf("%d",&t);
        //update data.
        while(t>=num)
        {
            if(!b.empty())
            {
                multiset<int>::iterator b_it=b.end();
                --b_it;   //这里一定要注意!调了好久的bug。要切记,C++有太多的东西是左闭右开的了!
                if(hehe[num]<*b_it)
                {
                    int temp=*b_it;
                    b.erase(b_it);
                    b.insert(hehe[num]);
                    a.insert(temp);
                }
                else a.insert(hehe[num]);
            }
            else
            {
                a.insert(hehe[num]);
            }
            ++num;
        }
        //cout data
        multiset<int>::iterator a_it=a.begin();
        printf("%d\n",*a_it);
        //update multiset b
        int temp=*a_it;
        a.erase(a_it);
        b.insert(temp);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值