#include <iostream>
using namespace std;
int n, arr[200005], used[400005];
const int inf = 1e9 + 69;
int query(int id){
int rs = inf;
for(; id; id >>= 1){
if(used[id] != 1) rs = min(rs, arr[id]);
if(!used[id]) break;
if(id % 2 == 1 && used[id - 1]){
rs = min(rs, arr[id - 1]);
if(used[id - 1] == 1) break;
}
}
return rs;
}
void erase(int id, int val){
for(; id; id >>= 1){
if(arr[id] == val){
used[id] = 0;
break;
}
if(id % 2 == 1){
if(arr[id - 1] == val){
used[id - 1] = used[id] = 1;
break;
}else used[id - 1] = 0;
}
used[id] = 1;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> arr[i];
used[i] = 2;
}
used[1] = 0;
for(int i = 1; i <= n; i ++){
int rs = query(i);
int upl = (i * 2 <= n ? arr[i * 2] : inf);
int upr = (i * 2 + 1 <= n ? arr[i * 2 + 1] : inf);
int v = min(rs, min(upl, upr));
cout << v;
if(i == n) cout << endl;
else cout << " ";
if(rs == v){
used[i * 2] = used[i * 2 + 1] = 0;
erase(i, rs);
}else if(upl == v){
used[i * 2] = 1;
used[i * 2 + 1] = 0;
}else if(upr == v){
used[i * 2 + 1] = 1;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
11 ms |
1280 KB |
Output is correct |
17 |
Correct |
12 ms |
1280 KB |
Output is correct |
18 |
Correct |
19 ms |
1280 KB |
Output is correct |
19 |
Correct |
15 ms |
1280 KB |
Output is correct |
20 |
Correct |
13 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
11 ms |
1280 KB |
Output is correct |
17 |
Correct |
12 ms |
1280 KB |
Output is correct |
18 |
Correct |
19 ms |
1280 KB |
Output is correct |
19 |
Correct |
15 ms |
1280 KB |
Output is correct |
20 |
Correct |
13 ms |
1280 KB |
Output is correct |
21 |
Correct |
50 ms |
3960 KB |
Output is correct |
22 |
Correct |
50 ms |
3960 KB |
Output is correct |
23 |
Correct |
46 ms |
3960 KB |
Output is correct |
24 |
Correct |
59 ms |
3960 KB |
Output is correct |
25 |
Correct |
57 ms |
4088 KB |
Output is correct |