poj1442 Black Box

时间:2023-03-08 17:01:42

The Black Case 好啊!

首先,读题很艰难...

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

 #include <cstdio>
#include <queue>
const int N = ;
// poj 1442 黑箱
struct DeHeap {
std::priority_queue<int> DOWN;
std::priority_queue<int, std::vector<int>, std::greater<int> > UP;
inline void clear() {
while(!UP.empty()) {
UP.pop();
}
while(!DOWN.empty()) {
DOWN.pop();
}
return;
}
inline void insert(int x) {
if(DOWN.empty()) {
UP.push(x);
}
else if(x >= DOWN.top()) {
UP.push(x);
}
else {
DOWN.push(x);
UP.push(DOWN.top());
DOWN.pop();
}
return;
}
inline int get() {
int x = UP.top();
DOWN.push(x);
UP.pop();
return x;
}
}dh; int add[N]; int main() {
int n, m, x, T = ;
while(T--) {
dh.clear();
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &add[i]);
}
int k = ;
for(int i = ; i <= m; i++) {
scanf("%d", &x);
while(k <= x) {
dh.insert(add[k]);
k++;
}
printf("%d\n", dh.get());
}
printf("\n");
}
return ;
}

AC代码