#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 (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]<<" ";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |