POJ-1442 Black Box(手写堆优化)

Posted MichaelZona

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-1442 Black Box(手写堆优化)相关的知识,希望对你有一定的参考价值。

Black Box

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 11150

 

Accepted: 4554

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

Sample Output

3
3
1
2

Source

​Northeastern Europe 1996​

 

心累到现在都没过……

分析:因为输出时是按照先输出最小的,再输出第二小这样的方式输出的,相当于依次输出一个有序序列中的值。但因为这个序列不是固定不变的,而是不断的在更新,所以用数组是无法实现的。我们可以用优先队列来做。

定义两个优先队列,一个用来存储前k小的数,大数在前,小数在后;另一个优先队列第k+1小到最大的数,小数在前,大数在后。每次拿到一个数,先判断第一个优先队列中的数满不满k个,如果不满k个,则直接把这个数压入到第一个队列;如果满k个,判断这个数和第一个优先队列中的第一个数的大小:如果比第一个数大,就压入第二个优先队列;如果比第一个数小,就把第一个优先队列的队首元素弹出压入第二个队列,把这个新数压入第一个优先队列。

输出时,如果第一个优先队列里的元素个数小于k,则先把第二个优先队列里的队首元素弹出压入第一个优先队列,然后输出第一个优先队列的队首元素;如果满k个,则直接输出第一个优先队列的队首元素。

1 #include "bits/stdc++.h"
2 #define mem(a,b) memset(a,b,sizeof(a))
3 using namespace std;
4 typedef long long LL;
5 const int MAX=30005;
6 int n,m;
7 int a[MAX],r[MAX];
8 struct Que
9 int h[MAX*10];
10 int n;
11 void ini()mem(h,0),n=0;
12 void heapify1(int x)
13 int child=x*2,key=h[x];
14 while (child<=n)
15 if (child<n && h[child]<h[child+1]) child++;
16 if (key<h[child])h[x]=h[child];x=child;child=x*2;
17 else break;
18
19 h[x]=key;
20
21 void insert1(int key)
22 int x=++n;
23 while (x>1)
24 if (key>h[x/2]) h[x]=h[x/2],x/=2;
25 else break;
26
27 h[x]=key;
28
29 void del1()
30 if (n==1) n=0;
31 else h[1]=h[n--],heapify1(1);
32
33 void heapify2(int x)
34 int child=x*2,key=h[x];
35 while (child<=n)
36 if (child<n && h[child]>h[child+1]) child++;
37 if (h[x]>h[child])h[x]=h[child];x=child;child=x*2;
38 else break;
39
40 h[x]=key;
41
42 void insert2(int key)
43 int x=++n;
44 while (x>1)
45 if (key<h[x/2]) h[x]=h[x/2],x/=2;
46 else break;
47
48 h[x]=key;
49
50 void del2()
51 if (n==1) n=0;
52 else h[1]=h[n--],heapify2(1);
53
54 q1,q2;
55 int main()
56 freopen ("box.in","r",stdin);
57 freopen ("box.out","w",stdout);
58 int i,j,k;
59 while (~scanf("%d%d",&n,&m))
60 q1.ini();q2.ini();
61 for (i=1;i<=n;i++)
62 scanf("%d",a+i);
63 for (j=1;j<=m;j++)
64 scanf("%d",r+j);
65 sort(r+1,r+m+1);
66 int index(1);
67 for (i=1;i<=n;i++)
68 if (index>n)
69 break;
70 if (q1.n<index)
71 q1.insert1(a[i]);
72 else if (a[i]<q1.h[1])
73 int zt=q1.h[1];
74 q1.del1();
75 q1.insert1(a[i]);
76 q2.insert2(zt);
77
78 else
79 q2.insert2(a[i]);
80 while (i==r[index])
81 printf("%d\\n",q1.h[1]);
82 if (q2.n>0)
83 q1.insert1(q2.h[1]);
84 q2.del2();
85
86 index++;
87
88
89
90 return 0;
91

 

以上是关于POJ-1442 Black Box(手写堆优化)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1442 Black Box(treap树)

Black Box POJ - 1442

POJ 1442 Black Box(优先队列)

优先队列(堆)经典例题——poj1442 black fox

优先队列(堆)经典例题——poj1442 black fox

A - Black Box 优先队列