forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1442(AC).cpp
81 lines (74 loc) · 1.22 KB
/
POJ1442(AC).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
using namespace std;
int a[31000];
int u[31000];
struct st_big{
public:
int x;
bool operator < (const st_big &a) const
{
return x < a.x;
}
};
struct st_small{
public:
int x;
bool operator < (const st_small &a) const
{
return x > a.x;
}
};
//the greater numbers are stored in the minimal heap
priority_queue<st_big> big;
//the smalll numbers are stored in the maximal heap
priority_queue<st_small> small;
int m, n;
int main()
{
int i, j;
int c;
st_big b;
st_small s;
while(scanf("%d%d", &m, &n) == 2){
while(!big.empty()){
big.pop();
}
while(!small.empty()){
small.pop();
}
for(i = 0; i < m; ++i){
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i){
scanf("%d", &u[i]);
}
j = 0;
for(i = 0; i < n; ++i){
while(j < u[i]){
s.x = a[j];
small.push(s);
while(!big.empty() && (s = small.top()).x < (b = big.top()).x){
c = s.x;
s.x = b.x;
b.x = c;
small.pop();
big.pop();
small.push(s);
big.push(b);
}
++j;
}
s = small.top();
printf("%d\n", s.x);
b.x = s.x;
small.pop();
big.push(b);
}
}
return 0;
}