#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;
typedef pair<int, int> pi;
map<pi,int> bes;
int n, p[200001];
int get(int ind, int y) {
if (bes.count({ind,y})) return bes[{ind,y}];
if (2*ind > n) return ind;
if (2*ind+1 > n) {
if (y > p[2*ind]) return 2*ind;
return ind;
}
if (y < min(p[2*ind],p[2*ind+1])) return bes[{ind,y}] = ind;
if (p[2*ind] < min(y,p[2*ind+1])) return bes[{ind,y}] = get(2*ind,y);
int mn = min(y,p[2*ind]);
if (get(2*ind,mn) < get(2*ind+1,mn)) {
if (mn == y) return bes[{ind,y}] = get(2*ind,y);
return bes[{ind,y}] = get(2*ind+1,y);
} else {
if (mn == y) return bes[{ind,y}] = get(2*ind+1,y);
return bes[{ind,y}] = get(2*ind,y);
}
}
void solve(int ind) {
if (2*ind > n) return;
if (2*ind+1 > n) {
if (p[ind] > p[2*ind]) swap(p[ind],p[2*ind]);
return;
}
if (p[ind] < min(p[2*ind],p[2*ind+1])) {
} else if (p[2*ind] < min(p[ind],p[2*ind+1])) {
swap(p[2*ind],p[ind]);
} else {
int mn = min(p[ind],p[2*ind]), mx = max(p[ind],p[2*ind]);
p[ind] = p[2*ind+1];
if (get(2*ind,mn) < get(2*ind+1,mn)) {
p[2*ind] = mn, p[2*ind+1] = mx;
} else {
p[2*ind] = mx, p[2*ind+1] = mn;
}
}
solve(ind+1);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
FOR(i,1,n+1) cin >> p[i];
solve(1);
FOR(i,1,n+1) cout << p[i] << " ";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
376 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
376 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
376 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
19 ms |
2028 KB |
Output is correct |
17 |
Correct |
18 ms |
2028 KB |
Output is correct |
18 |
Correct |
18 ms |
2028 KB |
Output is correct |
19 |
Correct |
38 ms |
3948 KB |
Output is correct |
20 |
Correct |
38 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
376 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
19 ms |
2028 KB |
Output is correct |
17 |
Correct |
18 ms |
2028 KB |
Output is correct |
18 |
Correct |
18 ms |
2028 KB |
Output is correct |
19 |
Correct |
38 ms |
3948 KB |
Output is correct |
20 |
Correct |
38 ms |
3948 KB |
Output is correct |
21 |
Correct |
80 ms |
7020 KB |
Output is correct |
22 |
Correct |
80 ms |
7020 KB |
Output is correct |
23 |
Correct |
81 ms |
7020 KB |
Output is correct |
24 |
Correct |
170 ms |
14828 KB |
Output is correct |
25 |
Correct |
172 ms |
14828 KB |
Output is correct |