#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000;
int n, A[maxn+10];
vector<int> dp[maxn+10][36], tmp1, tmp2;
void merge(vector<int> &res, int root, vector<int> &L, vector<int> &R) {
res.push_back(root);
int lidx = 0, ridx = 0;
int cur = 1;
while (lidx < L.size() || ridx < R.size()) {
for (int i = 0; i < cur; i++) {
if (lidx < L.size()) res.push_back(L[lidx++]);
}
for (int i = 0; i < cur; i++) {
if (ridx < R.size()) res.push_back(R[ridx++]);
}
cur *= 2;
}
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n; for (int i = 1; i <= n; i++) cin >> A[i];
for (int i = n; i; i--) {
int l = i*2, r = i*2+1;
for (int j = 0; j < 36; j++) {
if (j == 1) continue; // messy code == j=1 not needed
int idx = i, p = j;
while (p >= 2) {
p -= 2;
idx /= 2;
}
if (p == 1) idx *= 2;
if (idx == 0) continue;
int cur = A[idx];
if (l > n && r > n) {
dp[i][j].push_back(cur);
continue;
} else if (r > n) {
if (A[l] < cur) {
dp[i][j].push_back(A[l]);
dp[i][j].push_back(cur);
} else {
dp[i][j].push_back(cur);
dp[i][j].push_back(A[l]);
}
continue;
}
int L = A[l], R = A[r];
// no swap
if (cur < L && cur < R) {
merge(dp[i][j], cur, dp[l][0], dp[r][0]);
continue;
}
// swap left
if (cur > L && L < R) {
merge(dp[i][j], L, dp[l][j+2], dp[r][0]);
continue;
}
if (R < cur && R < L) {
tmp1 = {}, tmp2 = {};
// swap right
merge(tmp1, R, dp[l][0], dp[r][j+2]);
// swap both
merge(tmp2, R, dp[l][j+2], dp[r][3]);
dp[i][j] = min(tmp1, tmp2);
}
}
if (l <= n) {
for (int j = 0; j < 36; j++) vector<int>().swap(dp[l][j]);
}
if (r <= n) {
for (int j = 0; j < 36; j++) vector<int>().swap(dp[r][j]);
}
}
for (int v : dp[1][0]) cout << v << " ";
cout << "\n";
return 0;
}
Compilation message
swap.cpp: In function 'void merge(std::vector<int>&, int, std::vector<int>&, std::vector<int>&)':
swap.cpp:14:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (lidx < L.size() || ridx < R.size()) {
~~~~~^~~~~~~~~~
swap.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (lidx < L.size() || ridx < R.size()) {
~~~~~^~~~~~~~~~
swap.cpp:16:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (lidx < L.size()) res.push_back(L[lidx++]);
~~~~~^~~~~~~~~~
swap.cpp:19:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (ridx < R.size()) res.push_back(R[ridx++]);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
169464 KB |
Output is correct |
2 |
Correct |
104 ms |
169464 KB |
Output is correct |
3 |
Correct |
112 ms |
169464 KB |
Output is correct |
4 |
Correct |
101 ms |
169592 KB |
Output is correct |
5 |
Correct |
102 ms |
169464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
169464 KB |
Output is correct |
2 |
Correct |
104 ms |
169464 KB |
Output is correct |
3 |
Correct |
112 ms |
169464 KB |
Output is correct |
4 |
Correct |
101 ms |
169592 KB |
Output is correct |
5 |
Correct |
102 ms |
169464 KB |
Output is correct |
6 |
Correct |
100 ms |
169544 KB |
Output is correct |
7 |
Correct |
101 ms |
169464 KB |
Output is correct |
8 |
Correct |
116 ms |
169464 KB |
Output is correct |
9 |
Correct |
101 ms |
169464 KB |
Output is correct |
10 |
Correct |
102 ms |
169464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
169464 KB |
Output is correct |
2 |
Correct |
104 ms |
169464 KB |
Output is correct |
3 |
Correct |
112 ms |
169464 KB |
Output is correct |
4 |
Correct |
101 ms |
169592 KB |
Output is correct |
5 |
Correct |
102 ms |
169464 KB |
Output is correct |
6 |
Correct |
100 ms |
169544 KB |
Output is correct |
7 |
Correct |
101 ms |
169464 KB |
Output is correct |
8 |
Correct |
116 ms |
169464 KB |
Output is correct |
9 |
Correct |
101 ms |
169464 KB |
Output is correct |
10 |
Correct |
102 ms |
169464 KB |
Output is correct |
11 |
Correct |
109 ms |
169720 KB |
Output is correct |
12 |
Correct |
103 ms |
169720 KB |
Output is correct |
13 |
Correct |
111 ms |
169848 KB |
Output is correct |
14 |
Correct |
119 ms |
169720 KB |
Output is correct |
15 |
Correct |
115 ms |
169848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
169464 KB |
Output is correct |
2 |
Correct |
104 ms |
169464 KB |
Output is correct |
3 |
Correct |
112 ms |
169464 KB |
Output is correct |
4 |
Correct |
101 ms |
169592 KB |
Output is correct |
5 |
Correct |
102 ms |
169464 KB |
Output is correct |
6 |
Correct |
100 ms |
169544 KB |
Output is correct |
7 |
Correct |
101 ms |
169464 KB |
Output is correct |
8 |
Correct |
116 ms |
169464 KB |
Output is correct |
9 |
Correct |
101 ms |
169464 KB |
Output is correct |
10 |
Correct |
102 ms |
169464 KB |
Output is correct |
11 |
Correct |
109 ms |
169720 KB |
Output is correct |
12 |
Correct |
103 ms |
169720 KB |
Output is correct |
13 |
Correct |
111 ms |
169848 KB |
Output is correct |
14 |
Correct |
119 ms |
169720 KB |
Output is correct |
15 |
Correct |
115 ms |
169848 KB |
Output is correct |
16 |
Correct |
362 ms |
193528 KB |
Output is correct |
17 |
Correct |
370 ms |
193660 KB |
Output is correct |
18 |
Correct |
364 ms |
193528 KB |
Output is correct |
19 |
Correct |
371 ms |
193528 KB |
Output is correct |
20 |
Correct |
340 ms |
193528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
169464 KB |
Output is correct |
2 |
Correct |
104 ms |
169464 KB |
Output is correct |
3 |
Correct |
112 ms |
169464 KB |
Output is correct |
4 |
Correct |
101 ms |
169592 KB |
Output is correct |
5 |
Correct |
102 ms |
169464 KB |
Output is correct |
6 |
Correct |
100 ms |
169544 KB |
Output is correct |
7 |
Correct |
101 ms |
169464 KB |
Output is correct |
8 |
Correct |
116 ms |
169464 KB |
Output is correct |
9 |
Correct |
101 ms |
169464 KB |
Output is correct |
10 |
Correct |
102 ms |
169464 KB |
Output is correct |
11 |
Correct |
109 ms |
169720 KB |
Output is correct |
12 |
Correct |
103 ms |
169720 KB |
Output is correct |
13 |
Correct |
111 ms |
169848 KB |
Output is correct |
14 |
Correct |
119 ms |
169720 KB |
Output is correct |
15 |
Correct |
115 ms |
169848 KB |
Output is correct |
16 |
Correct |
362 ms |
193528 KB |
Output is correct |
17 |
Correct |
370 ms |
193660 KB |
Output is correct |
18 |
Correct |
364 ms |
193528 KB |
Output is correct |
19 |
Correct |
371 ms |
193528 KB |
Output is correct |
20 |
Correct |
340 ms |
193528 KB |
Output is correct |
21 |
Runtime error |
313 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
22 |
Halted |
0 ms |
0 KB |
- |