POJ 1442 Black Box

Description

Our Black Box represents aprimitive 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 Boxprocesses 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 inthe Black Box. Keep in mind that i-minimum is a number located at i-th placeafter 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 bynon-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 sequenceof transactions. The maximum number of ADD and GET transactions: 30000 of eachtype.


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 intoBlack Box. A values are integers not exceeding 2 000 000 000 by their absolutevalue, 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 arebeing included into Black Box at the moment of first, second, ... andN-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 factthat for the p-element of our u sequence we perform a GET transaction givingp-minimum number from our A(1), A(2), ..., A(u(p)) sequence.

Input

Input contains (in givenorder): M, N, A(1), A(2), ..., A(M), u(1), u(2), ..., u(N). All numbers aredivided by spaces and (or) carriage return characters.

Output

Write to the output Black Boxanswers 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

题目简介:这道题的题意不是很好理解。看了好久好久才看懂啊!!!!!!说一说题意吧。意思是,每行第一个数(假设是count吧)不参加排序,相当于一个计数器,每进行一次GET操作之前就+1,这个数起始为0。另外一个操作是ADD(x),就是往队列里面加入x,加入后把队列按从小到大排序。输入n,m分别表示进行ADD的次数和GET的次数。然后输入ADD的n个数,接着输入GET的时候,一旦ADD的次数等于这个数就进行GET,比如,1,2,6,6就是ADD进行1次,2次,6次,6次时分别进行一次GET,GET操作取第count个数。即取当前队列第count小的数(其实就是d当前队列第1、2、3...小的数)。

方法:堆排序。这道题对于我的意义在于第一次用了优先队列。。。。。。。方法其实还是很好想的。就是用两个队列来维护。一个队列从小到大q2,一个从大到小q1。然后每次GET就取从小到大的队头就完了。放的时候,就比较放入的元素和q1、q2队头的大小。不细说了,看代码应该可以理解。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#include<functional>
using namespace std;

int a[300010];

int main()
{
	priority_queue< int ,vector<int>,greater<int> > q1;//从大到小
	priority_queue< int,vector<int>,less<int> > q2;//从小到大
	int m ,n ,u;
	int i;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(i = 1;i<=m;i++)
		{
			scanf("%d",&a[i]);
		}
		int j = 1 ,t;
		for(i = 1;i<=n;i++)
		{
			scanf("%d",&u);
			while(j<=u)
			{
				if(q2.size() >= i)
				{
					if(q2.top() > a[j])
					{
						t = q2.top();
						q2.pop();
						q2.push(a[j]);
						q1.push(t);
					}
					else
					{
						q1.push(a[j]);
					}
				}
				else
				{
					if(!q1.empty()&&q1.top() < a[j])
					{
						t = q1.top();
						q1.pop();
						q1.push(a[j]);
						q2.push(t);
					}
					else
					{
						q2.push(a[j]);
					}
				}
				j++;
			}
			printf("%d\n",q2.top());
			if(!q1.empty())
			{
				q2.push(q1.top());
				q1.pop();
			}
		}
	}
	return 0;
}


 

你可能感兴趣的:(POJ 1442 Black Box)