#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int INF = 1e9;
int N;
int A[MAXN];
map<int, int> dp[MAXN]; // dp[n][x] = minimum sequence in subtree of n if the root's value is x
map<int, int> track[MAXN]; // we only keep track of where min(cur, lc, rc)'s value end up in the dp
int Dp(int n, int x) {
if (n > N) return INF;
if (dp[n].count(x)) return dp[n][x];
static vector<vector<int>> a_(MAXN, vector<int>(3));
auto &a = a_[n];
a[0] = x;
a[1] = (n * 2 + 0 <= N ? A[n * 2 + 0] : INF);
a[2] = (n * 2 + 1 <= N ? A[n * 2 + 1] : INF);
if (a[0] < min(a[1], a[2])) {
track[n][x] = 0;
return dp[n][x] = n;
}
if (a[1] < min(a[0], a[2])) {
track[n][x] = 1;
return dp[n][x] = Dp(n * 2, a[0]);
}
if (a[2] < min(a[0], a[1])) {
int xl = Dp(n * 2 + 0, a[0]);
int xr = Dp(n * 2 + 1, a[0]);
int yl = Dp(n * 2 + 0, a[1]);
int yr = Dp(n * 2 + 1, a[1]);
if (min(xl, yr) < min(xr, yl)) {
track[n][x] = 3;
return dp[n][x] = xl;
}
if (min(xl, yr) > min(xr, yl)) {
track[n][x] = 2;
return dp[n][x] = xr;
}
if (a[0] < a[1]) {
if (xl < xr) {
track[n][x] = 3;
return dp[n][x] = xl;
} else {
track[n][x] = 2;
return dp[n][x] = xr;
}
} else {
if (yr < yl) {
track[n][x] = 3;
return dp[n][x] = xl;
} else {
track[n][x] = 2;
return dp[n][x] = xr;
}
}
}
return -1;
}
void Trace() {
for (int n = N; n >= 1; n--) {
for (int i = n; i > 0; i /= 2) {
Dp(n, A[i ^ 0]);
Dp(n, A[i ^ 1]);
}
}
for (int n = 1; n <= N; n++) {
int x = A[n];
if (n * 2 + 0 <= N && track[n][x] & 1) swap(A[n], A[n * 2 + 0]);
if (n * 2 + 1 <= N && track[n][x] & 2) swap(A[n], A[n * 2 + 1]);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
}
Trace();
for (int i = 1; i <= N; i++) {
if (i > 1) {
cout << " ";
}
cout << A[i];
}
cout << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
30080 KB |
Output is correct |
2 |
Correct |
24 ms |
30072 KB |
Output is correct |
3 |
Correct |
24 ms |
30072 KB |
Output is correct |
4 |
Correct |
24 ms |
29952 KB |
Output is correct |
5 |
Correct |
24 ms |
30080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
30080 KB |
Output is correct |
2 |
Correct |
24 ms |
30072 KB |
Output is correct |
3 |
Correct |
24 ms |
30072 KB |
Output is correct |
4 |
Correct |
24 ms |
29952 KB |
Output is correct |
5 |
Correct |
24 ms |
30080 KB |
Output is correct |
6 |
Correct |
24 ms |
30208 KB |
Output is correct |
7 |
Correct |
23 ms |
30080 KB |
Output is correct |
8 |
Correct |
24 ms |
30080 KB |
Output is correct |
9 |
Correct |
24 ms |
30200 KB |
Output is correct |
10 |
Correct |
23 ms |
30080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
30080 KB |
Output is correct |
2 |
Correct |
24 ms |
30072 KB |
Output is correct |
3 |
Correct |
24 ms |
30072 KB |
Output is correct |
4 |
Correct |
24 ms |
29952 KB |
Output is correct |
5 |
Correct |
24 ms |
30080 KB |
Output is correct |
6 |
Correct |
24 ms |
30208 KB |
Output is correct |
7 |
Correct |
23 ms |
30080 KB |
Output is correct |
8 |
Correct |
24 ms |
30080 KB |
Output is correct |
9 |
Correct |
24 ms |
30200 KB |
Output is correct |
10 |
Correct |
23 ms |
30080 KB |
Output is correct |
11 |
Correct |
29 ms |
31736 KB |
Output is correct |
12 |
Correct |
29 ms |
31736 KB |
Output is correct |
13 |
Correct |
29 ms |
31736 KB |
Output is correct |
14 |
Correct |
29 ms |
31736 KB |
Output is correct |
15 |
Correct |
31 ms |
31744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
30080 KB |
Output is correct |
2 |
Correct |
24 ms |
30072 KB |
Output is correct |
3 |
Correct |
24 ms |
30072 KB |
Output is correct |
4 |
Correct |
24 ms |
29952 KB |
Output is correct |
5 |
Correct |
24 ms |
30080 KB |
Output is correct |
6 |
Correct |
24 ms |
30208 KB |
Output is correct |
7 |
Correct |
23 ms |
30080 KB |
Output is correct |
8 |
Correct |
24 ms |
30080 KB |
Output is correct |
9 |
Correct |
24 ms |
30200 KB |
Output is correct |
10 |
Correct |
23 ms |
30080 KB |
Output is correct |
11 |
Correct |
29 ms |
31736 KB |
Output is correct |
12 |
Correct |
29 ms |
31736 KB |
Output is correct |
13 |
Correct |
29 ms |
31736 KB |
Output is correct |
14 |
Correct |
29 ms |
31736 KB |
Output is correct |
15 |
Correct |
31 ms |
31744 KB |
Output is correct |
16 |
Correct |
566 ms |
168640 KB |
Output is correct |
17 |
Correct |
551 ms |
168696 KB |
Output is correct |
18 |
Correct |
579 ms |
168568 KB |
Output is correct |
19 |
Correct |
567 ms |
168568 KB |
Output is correct |
20 |
Correct |
553 ms |
168696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
30080 KB |
Output is correct |
2 |
Correct |
24 ms |
30072 KB |
Output is correct |
3 |
Correct |
24 ms |
30072 KB |
Output is correct |
4 |
Correct |
24 ms |
29952 KB |
Output is correct |
5 |
Correct |
24 ms |
30080 KB |
Output is correct |
6 |
Correct |
24 ms |
30208 KB |
Output is correct |
7 |
Correct |
23 ms |
30080 KB |
Output is correct |
8 |
Correct |
24 ms |
30080 KB |
Output is correct |
9 |
Correct |
24 ms |
30200 KB |
Output is correct |
10 |
Correct |
23 ms |
30080 KB |
Output is correct |
11 |
Correct |
29 ms |
31736 KB |
Output is correct |
12 |
Correct |
29 ms |
31736 KB |
Output is correct |
13 |
Correct |
29 ms |
31736 KB |
Output is correct |
14 |
Correct |
29 ms |
31736 KB |
Output is correct |
15 |
Correct |
31 ms |
31744 KB |
Output is correct |
16 |
Correct |
566 ms |
168640 KB |
Output is correct |
17 |
Correct |
551 ms |
168696 KB |
Output is correct |
18 |
Correct |
579 ms |
168568 KB |
Output is correct |
19 |
Correct |
567 ms |
168568 KB |
Output is correct |
20 |
Correct |
553 ms |
168696 KB |
Output is correct |
21 |
Runtime error |
571 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
22 |
Halted |
0 ms |
0 KB |
- |