#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+5, INF = 1e9;
int n;
int a[N], ans[N], state[N];
int get(int idx) {
int ret = INF;
while(idx > 0) {
if(state[idx] != 1) ret = min(ret, a[idx]);
if(state[idx] == 0) break;
if((idx&1) && state[idx^1] != 0) {
ret = min(ret, a[idx^1]);
if(state[idx^1] == 1) break;
}
idx >>= 1;
}
return ret;
}
void change(int idx, int val) {
while(idx > 0) {
if(a[idx] == val) {
state[idx] = 0;
return;
}
if(idx&1) {
if(a[idx^1] == val) {
state[idx^1] = state[idx] = 1;
return;
} else {
state[idx^1] = 0;
}
}
state[idx] = 1;
idx >>= 1;
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
fill_n(a, N, INF);
for(int i = 1; i <= n; i++) {
cin >> a[i];
state[i] = (i==1)?0:-1;
}
for(int i = 1; i <= n; i++) {
int x = get(i);
ans[i] = min({x, a[i<<1], a[i<<1|1]});
if(ans[i] == x) {
state[i<<1] = state[i<<1|1] = 0;
change(i, x);
} else if(ans[i] == a[i<<1]) {
state[i<<1] = 1, state[i<<1|1] = 0;
} else {
state[i<<1|1] = 1;
}
}
for(int i = 1; i <= n; i++)
cout << ans[i] << " \n"[i==n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2304 KB |
Output is correct |
2 |
Correct |
2 ms |
2304 KB |
Output is correct |
3 |
Correct |
1 ms |
2304 KB |
Output is correct |
4 |
Correct |
2 ms |
2304 KB |
Output is correct |
5 |
Correct |
2 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2304 KB |
Output is correct |
2 |
Correct |
2 ms |
2304 KB |
Output is correct |
3 |
Correct |
1 ms |
2304 KB |
Output is correct |
4 |
Correct |
2 ms |
2304 KB |
Output is correct |
5 |
Correct |
2 ms |
2304 KB |
Output is correct |
6 |
Correct |
2 ms |
2304 KB |
Output is correct |
7 |
Correct |
2 ms |
2304 KB |
Output is correct |
8 |
Correct |
2 ms |
2304 KB |
Output is correct |
9 |
Correct |
2 ms |
2304 KB |
Output is correct |
10 |
Correct |
2 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2304 KB |
Output is correct |
2 |
Correct |
2 ms |
2304 KB |
Output is correct |
3 |
Correct |
1 ms |
2304 KB |
Output is correct |
4 |
Correct |
2 ms |
2304 KB |
Output is correct |
5 |
Correct |
2 ms |
2304 KB |
Output is correct |
6 |
Correct |
2 ms |
2304 KB |
Output is correct |
7 |
Correct |
2 ms |
2304 KB |
Output is correct |
8 |
Correct |
2 ms |
2304 KB |
Output is correct |
9 |
Correct |
2 ms |
2304 KB |
Output is correct |
10 |
Correct |
2 ms |
2304 KB |
Output is correct |
11 |
Correct |
2 ms |
2304 KB |
Output is correct |
12 |
Correct |
3 ms |
2304 KB |
Output is correct |
13 |
Correct |
2 ms |
2304 KB |
Output is correct |
14 |
Correct |
2 ms |
2304 KB |
Output is correct |
15 |
Correct |
3 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2304 KB |
Output is correct |
2 |
Correct |
2 ms |
2304 KB |
Output is correct |
3 |
Correct |
1 ms |
2304 KB |
Output is correct |
4 |
Correct |
2 ms |
2304 KB |
Output is correct |
5 |
Correct |
2 ms |
2304 KB |
Output is correct |
6 |
Correct |
2 ms |
2304 KB |
Output is correct |
7 |
Correct |
2 ms |
2304 KB |
Output is correct |
8 |
Correct |
2 ms |
2304 KB |
Output is correct |
9 |
Correct |
2 ms |
2304 KB |
Output is correct |
10 |
Correct |
2 ms |
2304 KB |
Output is correct |
11 |
Correct |
2 ms |
2304 KB |
Output is correct |
12 |
Correct |
3 ms |
2304 KB |
Output is correct |
13 |
Correct |
2 ms |
2304 KB |
Output is correct |
14 |
Correct |
2 ms |
2304 KB |
Output is correct |
15 |
Correct |
3 ms |
2304 KB |
Output is correct |
16 |
Correct |
12 ms |
3456 KB |
Output is correct |
17 |
Correct |
13 ms |
3456 KB |
Output is correct |
18 |
Correct |
15 ms |
3456 KB |
Output is correct |
19 |
Correct |
15 ms |
3452 KB |
Output is correct |
20 |
Correct |
19 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2304 KB |
Output is correct |
2 |
Correct |
2 ms |
2304 KB |
Output is correct |
3 |
Correct |
1 ms |
2304 KB |
Output is correct |
4 |
Correct |
2 ms |
2304 KB |
Output is correct |
5 |
Correct |
2 ms |
2304 KB |
Output is correct |
6 |
Correct |
2 ms |
2304 KB |
Output is correct |
7 |
Correct |
2 ms |
2304 KB |
Output is correct |
8 |
Correct |
2 ms |
2304 KB |
Output is correct |
9 |
Correct |
2 ms |
2304 KB |
Output is correct |
10 |
Correct |
2 ms |
2304 KB |
Output is correct |
11 |
Correct |
2 ms |
2304 KB |
Output is correct |
12 |
Correct |
3 ms |
2304 KB |
Output is correct |
13 |
Correct |
2 ms |
2304 KB |
Output is correct |
14 |
Correct |
2 ms |
2304 KB |
Output is correct |
15 |
Correct |
3 ms |
2304 KB |
Output is correct |
16 |
Correct |
12 ms |
3456 KB |
Output is correct |
17 |
Correct |
13 ms |
3456 KB |
Output is correct |
18 |
Correct |
15 ms |
3456 KB |
Output is correct |
19 |
Correct |
15 ms |
3452 KB |
Output is correct |
20 |
Correct |
19 ms |
3456 KB |
Output is correct |
21 |
Correct |
53 ms |
7208 KB |
Output is correct |
22 |
Correct |
54 ms |
7288 KB |
Output is correct |
23 |
Correct |
49 ms |
7160 KB |
Output is correct |
24 |
Correct |
59 ms |
7160 KB |
Output is correct |
25 |
Correct |
59 ms |
7288 KB |
Output is correct |