Black Box
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10376 | Accepted: 4275 |
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
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.
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
优先队列:
注意下面的>> 和 > >和区别,优先队列需要有空格,不然就是右移运算符了
greater 表示从小到大
less 表示从大到小
priority_queue<int,vector<int>,greater<int> >small;
priority_queue<int,vector<int>,less<int> >big;
顶端的数字用top表示
small.top();
没有small . front( ) ;
题意:
给出一些n个数字(序列一),和m个数字(序列二)
将n个数字按顺序加入一个盒子中
求当序列二中的其中的数字为x时,且位置为2;表示序列一中,加个x个数字的时候,第 二小的数字是哪一个
如
1 2 6 61 表示求加入一个数字的时候第一小的数字
2 表示求加入二个数字的时候第二小的数字
6 表示求加入六个数字的时候第三小的数字
6 表示求加入六个数字的时候第四小的数字
题解:
这里维护两个优先队列,一个最大一个最小
最大优先队列的所有数字 一定要比 最小优先队列中所有数字 要小
因此我们可以维护最大优先队列的个数来保证最小优先队列的顶端一定是第几最小的
例如:
最大优先队列中数字有两个,由于最大优先队列中所有的数字比最小优先队列中所有数字都要小
因此最小优先队列的顶端一定是第三小的数字
每一次输出一个数字时才在最大优先队列中加入一个数字;
因为当最小优先队列中的顶端的是第 n 个最小数字的时候,可以保证最大优先队列中有 n-1 个数字
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 30010
int a[MAXN];
int main()
{
int n,m;
//freopen("in.txt","r",stdin);
priority_queue<int,vector<int>,greater<int> >small;
priority_queue<int,vector<int>,less<int> >big;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int cnt=1;
int temp;
for(int i=0;i<m;i++){
scanf("%d",&temp);
while(temp>=cnt)
{
small.push(a[cnt++]);
if(big.size()&&small.top()<big.top()){
int num1=small.top();
int num2=big.top();
big.pop();
small.pop();
big.push(num1);
small.push(num2);
}
}
printf("%d\n",small.top());
big.push(small.top());
small.pop();
}
return 0;
}