#include <bits/stdc++.h>
using namespace std;
int n, A[200005]; map<pair<int, int>, int> swp;
int dp(int i, int j){ int l = i*2, r = i*2+1, ret = i;
if (swp.count({i, j})) return swp[{i, j}];
else if (l > n){}
else if (r > n) ret = (A[l] < j) ? l : i;
else if (A[l] < min(j, A[r])) ret = dp(l, j);
else if (A[r] < min(j, A[l])){
int mn = min(j, A[l]);
if (dp(l, mn) < dp(r, mn)) ret = ((mn == j) ? dp(l, j) : dp(r, j));
else ret = ((mn == j) ? dp(r, j) : dp(l, j));
}return swp[{i, j}] = ret;
}
int main(){
cin >> n; for (int i = 1; i <= n; i++) cin >> A[i];
for (int i = 1; i <= n; i++){ int l = i*2, r = i*2+1;
if (l > n){ continue; }
else if (r > n){ if (A[l] < A[i]) swap(A[l], A[i]); }
else if (A[l] < min(A[i], A[r])) swap(A[l], A[i]);
else if (A[r] < min(A[i], A[l])){
int mn = min(A[i], A[l]), mx = max(A[i], A[l]);
A[i] = A[r];
if (dp(l, mn) < dp(r, mn)) tie(A[l], A[r]) = {mn, mx};
else tie(A[l], A[r]) = {mx, mn};
}
}for (int i = 1; i <= n; i++) cout<<A[i]<<" ";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
36 ms |
3248 KB |
Output is correct |
17 |
Correct |
38 ms |
3508 KB |
Output is correct |
18 |
Correct |
37 ms |
3316 KB |
Output is correct |
19 |
Correct |
75 ms |
7196 KB |
Output is correct |
20 |
Correct |
91 ms |
7164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
36 ms |
3248 KB |
Output is correct |
17 |
Correct |
38 ms |
3508 KB |
Output is correct |
18 |
Correct |
37 ms |
3316 KB |
Output is correct |
19 |
Correct |
75 ms |
7196 KB |
Output is correct |
20 |
Correct |
91 ms |
7164 KB |
Output is correct |
21 |
Correct |
152 ms |
12996 KB |
Output is correct |
22 |
Correct |
158 ms |
12964 KB |
Output is correct |
23 |
Correct |
190 ms |
12868 KB |
Output is correct |
24 |
Correct |
317 ms |
28432 KB |
Output is correct |
25 |
Correct |
315 ms |
28552 KB |
Output is correct |