Black Box(POJ--1442
2015-08-14 20:37
543 查看
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.
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.
题意:多组输入。输入m个数存入a数组,输出n次。m个数按输入顺序输入,根据n个数(将这n个数存入b数组),第i个数表示输入完前b[i]个数之后输出第i个小的数。
思路:将数组a按照b数组数值的个数往两个队列里存数,一个是升序队列一个是降序队列。
Sample Input
Sample Output
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.
题意:多组输入。输入m个数存入a数组,输出n次。m个数按输入顺序输入,根据n个数(将这n个数存入b数组),第i个数表示输入完前b[i]个数之后输出第i个小的数。
思路:将数组a按照b数组数值的个数往两个队列里存数,一个是升序队列一个是降序队列。
Sample Input
7 4 3 1 -4 2 8 -1000 2 1 2 6 6
Sample Output
3 3 1 2
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #define MAX 40000 using namespace std; int a[MAX],b[MAX]; int main() { //freopen("lalala.text","r",stdin); int m,n; while(~scanf("%d %d",&m,&n)) { priority_queue<int>first; //升序队列即队顶元素为队内最大值,该队列用以存第几小的数 priority_queue<int,vector<int>,greater<int> >second; //降序队列即队顶元素为队内最小值 for(int i=1; i<=m; i++) scanf("%d",&a[i]); for(int i=1; i<=n; i++) scanf("%d",&b[i]); int j=1; for(int i=1; i<=n; i++) //遍历b数组 { for(; j<=b[i]; j++) //开始往两个队列里放入数值 { if(first.size()<i) //如果第一个队列未填满就把当前a[j]放入第二个队列里,然后将第二个队列里最小的放到第一个队列里 { second.push(a[j]); first.push(second.top()); second.pop(); } else //如果第一个队列已经填满了就去拿当前的a[i]与第一队列最大的数去比较 { if(a[j]<first.top()) //如果当前的a[i]小于第一队列最大的数就将a[i]放到第一队列,将第一队列最大的数放到第二队列 { second.push(first.top()); first.pop(); first.push(a[j]); } else //如果当前的a[i]不小于第一队列最大的数就将当前的a[i]放到第二个队列 second.push(a[j]); } } while(first.size()<i) //如果第一个队列未填满就从第二个队列拿出最小的往第一个队列里添 { first.push(second.top()); second.pop(); } printf("%d\n",first.top()); } } return 0; }
相关文章推荐
- 猴子的智商
- 复旦大学高等代数课程学习资料推荐
- A* 寻路算法
- 从PHP 到JAVA
- Cookie和是Session
- 图的基本存储的基本方式一
- HDU-2084 数塔
- PPT自动载入图片并矩阵分布
- [转]XML中元素(Element)与节点(Node)的区别
- Oracle 学习之RAC(九) 集群负载均衡及故障转移
- 【xml】xml初步总结
- 计数排序
- 关于Hibernate中的普通增删改查
- 图的基本存储的基本方式二 (链表)
- POJ 2411 大矩形用1X2小矩形填充 状态dp DFS
- --acm的思考
- C语言第12轮:指针
- Codeforces Gym 100523K K - Cross Spider 计算几何,判断是否n点共面
- 快速排序详解
- 函数指针与typedef