Black Box(POJ--1442
来源:互联网 发布:外星人windows hello 编辑:程序博客网 时间:2024/06/11 18:11
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
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 43 1 -4 2 8 -1000 21 2 6 6
Sample Output
3312
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#define MAX 40000using 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;}
0 0
- poj 1442 Black Box
- POJ 1442 Black Box
- poj--1442--black box
- POJ 1442 Black Box
- POJ 1442 Black Box
- POJ 1442 Black Box
- POJ 1442 Black Box
- poj 1442 Black Box
- POJ 1442 Black Box
- POJ 1442 Black Box
- POJ 1442 Black Box
- poj 1442 Black Box
- POJ 1442 Black Box
- poj 1442 Black Box
- poj 1442 Black Box
- POJ 1442 Black Box
- 【POJ 1442】 Black Box
- poj 1442 Black Box
- dede整站迁移
- Spring---IOC原理浅析
- python:元组和小括号的误导
- 小白书之栈的调用过程
- C# 、Vb .Net 中 Datagridview显示行号方法。
- Black Box(POJ--1442
- Codeforces 19D(线段树+离散化)
- 安卓百度地图附近poi搜索以及到指定poi的换乘方案
- 共享队列ShareQueue
- hdu-5025 Saving Tang Monk (BFS + 状态压缩)
- php最简单的注入与最简单的避免方式
- 【OpenCV】高斯混合背景建模
- Codeforces 432C Prime Swaps
- 2015.8.14工作总结