poj1442 Black Box

The Black Case 好啊!

首先,读题很艰难...

读完题,发现是求第k小的数,那么我们用splay水过对顶堆水过即可。

 1 #include <cstdio>
 2 #include <queue>
 3 const int N = 300010;
 4 // poj 1442 黑箱
 5 struct DeHeap {
 6     std::priority_queue<int> DOWN;
 7     std::priority_queue<int, std::vector<int>, std::greater<int> > UP;
 8     inline void clear() {
 9         while(!UP.empty()) {
10             UP.pop();
11         }
12         while(!DOWN.empty()) {
13             DOWN.pop();
14         }
15         return;
16     }
17     inline void insert(int x) {
18         if(DOWN.empty()) {
19             UP.push(x);
20         }
21         else if(x >= DOWN.top()) {
22             UP.push(x);
23         }
24         else {
25             DOWN.push(x);
26             UP.push(DOWN.top());
27             DOWN.pop();
28         }
29         return;
30     }
31     inline int get() {
32         int x = UP.top();
33         DOWN.push(x);
34         UP.pop();
35         return x;
36     }
37 }dh;
38 
39 int add[N];
40 
41 int main() {
42     int n, m, x, T = 1;
43     while(T--) {
44         dh.clear();
45         scanf("%d%d", &n, &m);
46         for(int i = 1; i <= n; i++) {
47             scanf("%d", &add[i]);
48         }
49         int k = 1;
50         for(int i = 1; i <= m; i++) {
51             scanf("%d", &x);
52             while(k <= x) {
53                 dh.insert(add[k]);
54                 k++;
55             }
56             printf("%d\n", dh.get());
57         }
58         printf("\n");
59     }
60     return 0;
61 }
AC代码

 

posted @ 2018-06-07 17:45  garage  阅读(84)  评论(0编辑  收藏  举报