Algorithm学习笔记 --- Black Box

来源:互联网 发布:复制linux系统 编辑:程序博客网 时间:2024/05/29 13:33
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 6803
Accepted: 2749

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 43 1 -4 2 8 -1000 21 2 6 6

Sample Output

3312

Source

Northeastern Europe 1996


题意:
此题的意思是一个模拟的数据库黑盒子,里面可以进行数据操作(还有一些都是废话就不解释了),
M(要插入多少个数) N(当队列里面的数满足输入的时候第几次GET就输出第几个最小的数)



解题分析:

此题在刚看的时候我也被题里面的这么多英文吓住了,后来在网上找了找翻译,并且自己把给的那几组数据从重新在纸上理解了一下,发现明白了。大家来看:

1 ADD(3)      0 3//在没有元素的情况下输入一个3   2 GET         1 3                                    3 //此时里面有了一个元素是3,要弹出第一个小的元素,先观察里面的元素是否够多,不够多继续添加,一下以此类推,在这里可以看出里面的元素刚好一个正好弹出,并且弹出最小的一个元素就是3.3 ADD(1)      1 1, 3   //接下来有又添加一个元素1,里面的元素是1,34 GET         2 1, 3                                 3 //现在里面有两个元素,再次观察这里的元素是否够两个,这里正好是两个,并且弹出第二小的元素是3.5 ADD(-4)     2 -4, 1, 3   //接下来加入-4  里面是-4,1,3,在这里观察这里的元素是否够6个,若不够继续添加。若够就弹出第三次的GET(第几次GET就输出第几小的数)元素6 ADD(2)      2 -4, 1, 2, 3   //接下来加入2,里面是-4,1,2,3,元素还是不够7 ADD(8)      2 -4, 1, 2, 3, 8   //接下来添加8.里面一共5个元素,-4,1,2,3,8还是不够继续添加。8 ADD(-1000)  2 -1000, -4, 1, 2, 3, 8//接下来添加-1000,此时里面刚好6个,下一步就不用继续添加,   9 GET         3 -1000, -4, 1, 2, 3, 8                1//此时元素刚好是6个,在这里是第三个GET,那就弹出第三个数(此处已经排序) 10 GET       4 -1000, -4, 1, 2, 3, 8                2 //再此也是输出第四个数11 ADD(2)     4 -1000, -4, 1, 2, 2, 3, 8   
结束。
对于这列可以用单调队列(堆处理的数据可以用C++中的STL模板),比较简单。

代码:
#include<iostream>
#include<cstdio>
#include<queue>      //队列的头文件
#include<functional>//greater<>的头文件
#define MAXN 30005
using namespace std;
priority_queue<int, vector<int>, greater<int> > min_heap;//优先队列 按照由小到大顺序;
priority_queue<int> max_heap;                            //priority_queue调用 STL里面的 make_heap(), pop_heap(), push_heap();
int Add[MAXN],Get[MAXN];                                 //创建两个队列;
int main()
{
    int M,N;
    while(scanf("%d %d",&M,&N))
    {
        for(int i=1;i<=M;i++)
            scanf("%ld",&Add[i]);
        for(int i=1;i<=N;i++)
            scanf("%ld",&Get[i]);
        int flag=1;                                        //设计一个标志位
        Get[0]=0;                                           //初始化输出队列
        for(int i=1;i<=M;i++)
        {
            min_heap.push(Add[i]);                          //先放入Add队列
            if(!max_heap.empty())                           //判断是不是空的
            {
                if(max_heap.top()>min_heap.top())           //最大堆的堆顶比最小堆的堆顶大时
                {
                    max_heap.push(min_heap.top());              //吧对小队列的头弹出放到最大队列里进行比较
                    min_heap.pop();                             //把最小队列的头弹出
                    min_heap.push(max_heap.top());              //把最大队列的头部弹出放到最小队列里进行比较
                    max_heap.pop();                             //把最大队列的头弹出
                }
            }
            while(Get[flag]==(min_heap.size()+max_heap.size()))//达到get的时间时输出最小堆的堆顶
            {
                printf("%d\n",min_heap.top());
                flag++;                                         //flag表示题目中的i的含义
                while(max_heap.size()<flag-1)                   //保证最大堆里有flag-1个元素。
                {
                    max_heap.push(min_heap.top());
                    min_heap.pop();
                }
            }
        }   
}
    return 0;
}
0 0
原创粉丝点击