首页 > 代码库 > poj1442 Black Box 栈和优先队列

poj1442 Black Box 栈和优先队列

   题意:有n个数,按顺序加入,求加入前Gi个数时第i个最小的数是多少

思路:这里需要用到STL里的优先队列priority_queue,建一个大堆和一个小堆,若想在一个无序的序列里找第n个小的数,可以先把一个序列的n-1个数放入大堆(即假设这n-1个数是该序列里最小的),然后向小堆里push数,若小堆头元素<大堆头元素(即最小的元素比最大的元素大,说明大堆里的数还不是最小的),则交换两个数。
难点:这里如果只用一个小堆的话会超时;另外每输入的一个m数,要查询的最小的数正好是第i++个,即这里每输出一个数就把该数存入大堆里,正好使大堆里数的个数不断++(由0个不断增加),以方便下一次查询第i++个小的。





Black Box
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7205 Accepted: 2930

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 

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 43 1 -4 2 8 -1000 21 2 6 6

Sample Output

3312

 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstring> 7 #include <cmath> 8 #include <stack> 9 #include <queue>10 #include <functional>11 #include <vector>12 #include <map>13 //priority_queue<int> pq;14 //queue<int >q;15 using namespace std;16 17 #define M 0x0f0f0f0f18 #define min(a,b) (a>b?b:a)19 #define max(a,b) (a>b?a:b)20 int main()21 {22     int m,n;23     int i,j,b,c,t;24     int a[30005];25     scanf("%d %d",&n,&m);26     priority_queue< int,vector<int>,greater<int> >small;27     priority_queue< int>large;28     for(i=0; i<n; i++)29         scanf("%d",&a[i]);30     c=0;31     for(i=0; i<m; i++)32     {33         scanf("%d",&b);34         while(c<b)35         {36             small.push(a[c]);37             if(!large.empty()&&small.top()<large.top())38             {39                 t=small.top();40                 small.pop();41                 small.push(large.top());42                 large.pop();43                 large.push(t);44             }45             c++;46         }47         printf("%d\n",small.top());48         //精华!!!!49         large.push(small.top());//每次i++,让large里放一个数,使得以后每次比较时50         small.pop();           //保证small里的第一个数是第i个最小的!!!!!51     }52     return 0;53 }
View Code