【优先队列-求第Ki大的数】Black Box
2015-07-28 16:49
232 查看
[b]Black Box[/b]
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.
Sample Input
Sample Output
题目大意:
有多组测试数据,第一行输入N,Q,表示有N个数据要输入,有Q个询问。
第二行输入N个数,表示有一个(存在N个数据)的数据集。
第三行输入Q个数,设每个数位qi,第i个数位qi,表示输入这些数据集的前qi个数,求在这些数据集第i小的数据为多少;
解法:
由于每次询问的第K小的数是不固定的,K是递增的,可以采用两个堆(小顶堆和大顶堆)来实现数据的维护、
1.我们要保证的是让小顶堆的每一个元素都比大顶堆的中的每一个元素大。
2.保证大顶堆中有K个元素(从小顶堆取堆顶元素,直到大顶堆有K个元素),大顶堆的堆顶的元素既为所以数据的第N小的数据。
3。维护的话,只需要判断这小顶堆的堆顶元素是否大于大顶堆的堆顶元素,如果大于的话,就不用再交换,如果小于的话,需要交换两个堆顶元素然后再重复判断,直到,小顶堆的堆得堆顶元素都大于大顶堆的堆顶元素。
View Code
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8637 | Accepted: 3542 |
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 4 3 1 -4 2 8 -1000 2 1 2 6 6
Sample Output
3 3 1 2
题目大意:
有多组测试数据,第一行输入N,Q,表示有N个数据要输入,有Q个询问。
第二行输入N个数,表示有一个(存在N个数据)的数据集。
第三行输入Q个数,设每个数位qi,第i个数位qi,表示输入这些数据集的前qi个数,求在这些数据集第i小的数据为多少;
解法:
由于每次询问的第K小的数是不固定的,K是递增的,可以采用两个堆(小顶堆和大顶堆)来实现数据的维护、
1.我们要保证的是让小顶堆的每一个元素都比大顶堆的中的每一个元素大。
2.保证大顶堆中有K个元素(从小顶堆取堆顶元素,直到大顶堆有K个元素),大顶堆的堆顶的元素既为所以数据的第N小的数据。
3。维护的话,只需要判断这小顶堆的堆顶元素是否大于大顶堆的堆顶元素,如果大于的话,就不用再交换,如果小于的话,需要交换两个堆顶元素然后再重复判断,直到,小顶堆的堆得堆顶元素都大于大顶堆的堆顶元素。
#include <iostream> #include <queue> #include <stdio.h> #define MAX 30010 using namespace std; struct Node1 { int S; friend bool operator <(Node1 a,Node1 b) {return a.S>b.S;} }; struct Node2 { int S; friend bool operator <(Node2 a,Node2 b) {return a.S<b.S;} }; int main() { int N,M,i,j,k,Q,A,B,Sign,t=1; Node1 Num1; Node2 Num2; int NUM[MAX]; priority_queue<Node1>ID1;/*小顶堆*/ priority_queue<Node2>ID2;/*大顶堆*/ while(scanf("%d%d",&N,&M)!=EOF) { for(i=0;i<N;i++) { scanf("%d",&NUM[i]); } Sign=1;j=0; for(i=0;i<M;i++) { scanf("%d",&Q); while(j<Q&&j<N) { /*添加小顶堆元素*/ Num1.S=NUM[j++]; ID1.push(Num1); } for(k=ID2.size();k<Sign;k++) { /*求第K小的数,保证大顶堆有K个数*/ Num2.S=ID1.top().S;ID1.pop(); ID2.push(Num2); } while(ID2.size()>0&&ID1.top().S<ID2.top().S) { /*维护*/ Num1.S=ID2.top().S;ID2.pop(); ID1.push(Num1); Num2.S=ID1.top().S;ID1.pop(); ID2.push(Num2); } printf("%d\n",ID2.top().S); Sign++; } } return 0; }
View Code
相关文章推荐
- 2015-7-28 调试pcl
- HDU 2037:今年暑假不AC
- poj_1065_Wooden Sticks
- XP下安装Fedora17
- OC 学习第一天
- 登录的验证码实现
- UVA 1600 - Patrol Robot( BFS & DFS)
- svn提交静态库文件
- CGroup 介绍、应用实例及原理描述(已发表于IBM开发者论坛)
- 关于rtmpdump的windows下编译
- NYOJ 757 期末考试(优先队列)
- iOS通知问题之:一眼就看懂的NSNotificationCenter应用
- Number of Digit One
- AHandler机制
- iOS 检测设备旋转状态
- Python 获取根据文件URL 获取文件的基本信息
- 常用第三方库
- js中字符串的拼接的另一种方法
- ShopNC本地生活o2o网站的源代码,没有域名限制
- Xcode界面简介