#include <bits/stdc++.h>
using namespace std;
int a[200002];
bool needed[200002][18][2];
vector<int> dp[200002][18][2], tmp1, tmp2;
void merge(vector<int> &ret, vector<int> &l, vector<int> &r) {
for (int i = 0, j = 1; i < l.size(); i += j, j <<= 1) {
for (int k = i; k < i + j && k < l.size(); k++) ret.push_back(l[k]);
for (int k = i; k < i + j && k < r.size(); k++) ret.push_back(r[k]);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
needed[1][0][1] = 1;
for (int node = 1; node <= n / 2; node++) {
for (int i = 0; node >> i; i++)
for (int j : {0, 1}) {
if (!needed[node][i][j]) continue;
int mn = min({a[((node >> i + 1) << 1) + j], a[2 * node],
a[2 * node + 1]});
if (mn == a[((node >> i + 1) << 1) + j]) {
needed[2 * node][0][0] = needed[2 * node + 1][0][1] = 1;
} else if (mn == a[2 * node]) {
needed[2 * node][i + 1][j] = needed[2 * node + 1][0][1] = 1;
} else {
needed[2 * node][0][0] = needed[2 * node][i + 1][j] = 1;
needed[2 * node + 1][0][0] =
needed[2 * node + 1][i + 1][j] = 1;
}
}
}
for (int node = n; node; node--) {
for (int i = 0; node >> i; i++)
for (int j : {0, 1}) {
if (!needed[node][i][j]) continue;
int p = ((node >> i + 1) << 1) + j;
if (2 * node > n) dp[node][i][j] = {a[p]};
else if (2 * node == n) {
dp[node][i][j] = {min(a[p], a[2 * node]),
max(a[p], a[2 * node])};
} else {
if (a[2 * node + 1] < min(a[p], a[2 * node])) {
tmp1 = tmp2 = {a[2 * node + 1]};
merge(tmp1, dp[2 * node][0][0],
dp[2 * node + 1][i + 1][j]);
merge(tmp2, dp[2 * node][i + 1][j],
dp[2 * node + 1][0][0]);
dp[node][i][j] = min(tmp1, tmp2);
} else {
dp[node][i][j] = {min(a[p], a[2 * node])};
if (a[p] < a[2 * node]) {
merge(dp[node][i][j], dp[2 * node][0][0],
dp[2 * node + 1][0][1]);
} else {
merge(dp[node][i][j], dp[2 * node][i + 1][j],
dp[2 * node + 1][0][1]);
}
}
}
}
if (2 * node < n) {
for (int i = 0; 2 * node >> i; i++)
for (int j : {0, 1}) {
vector<int>().swap(dp[2 * node][i][j]);
vector<int>().swap(dp[2 * node + 1][i][j]);
}
}
}
for (int i : dp[1][0][1]) printf("%d ", i);
return 0;
}
Compilation message
swap.cpp: In function 'void merge(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
swap.cpp:9:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for (int i = 0, j = 1; i < l.size(); i += j, j <<= 1) {
| ~~^~~~~~~~~~
swap.cpp:10:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (int k = i; k < i + j && k < l.size(); k++) ret.push_back(l[k]);
| ~~^~~~~~~~~~
swap.cpp:11:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for (int k = i; k < i + j && k < r.size(); k++) ret.push_back(r[k]);
| ~~^~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:26:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mn = min({a[((node >> i + 1) << 1) + j], a[2 * node],
| ~~^~~
swap.cpp:28:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | if (mn == a[((node >> i + 1) << 1) + j]) {
| ~~^~~
swap.cpp:45:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int p = ((node >> i + 1) << 1) + j;
| ~~^~~
swap.cpp:17:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
swap.cpp:18:36: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | for (int i = 1; i <= n; i++) scanf("%d", a + i);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
172820 KB |
Output is correct |
2 |
Correct |
45 ms |
172884 KB |
Output is correct |
3 |
Correct |
39 ms |
172884 KB |
Output is correct |
4 |
Correct |
39 ms |
172792 KB |
Output is correct |
5 |
Correct |
46 ms |
172932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
172820 KB |
Output is correct |
2 |
Correct |
45 ms |
172884 KB |
Output is correct |
3 |
Correct |
39 ms |
172884 KB |
Output is correct |
4 |
Correct |
39 ms |
172792 KB |
Output is correct |
5 |
Correct |
46 ms |
172932 KB |
Output is correct |
6 |
Correct |
38 ms |
172892 KB |
Output is correct |
7 |
Correct |
40 ms |
172988 KB |
Output is correct |
8 |
Correct |
38 ms |
172896 KB |
Output is correct |
9 |
Correct |
38 ms |
172976 KB |
Output is correct |
10 |
Correct |
43 ms |
172952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
172820 KB |
Output is correct |
2 |
Correct |
45 ms |
172884 KB |
Output is correct |
3 |
Correct |
39 ms |
172884 KB |
Output is correct |
4 |
Correct |
39 ms |
172792 KB |
Output is correct |
5 |
Correct |
46 ms |
172932 KB |
Output is correct |
6 |
Correct |
38 ms |
172892 KB |
Output is correct |
7 |
Correct |
40 ms |
172988 KB |
Output is correct |
8 |
Correct |
38 ms |
172896 KB |
Output is correct |
9 |
Correct |
38 ms |
172976 KB |
Output is correct |
10 |
Correct |
43 ms |
172952 KB |
Output is correct |
11 |
Correct |
38 ms |
173020 KB |
Output is correct |
12 |
Correct |
39 ms |
172884 KB |
Output is correct |
13 |
Correct |
38 ms |
172884 KB |
Output is correct |
14 |
Correct |
42 ms |
172960 KB |
Output is correct |
15 |
Correct |
43 ms |
173148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
172820 KB |
Output is correct |
2 |
Correct |
45 ms |
172884 KB |
Output is correct |
3 |
Correct |
39 ms |
172884 KB |
Output is correct |
4 |
Correct |
39 ms |
172792 KB |
Output is correct |
5 |
Correct |
46 ms |
172932 KB |
Output is correct |
6 |
Correct |
38 ms |
172892 KB |
Output is correct |
7 |
Correct |
40 ms |
172988 KB |
Output is correct |
8 |
Correct |
38 ms |
172896 KB |
Output is correct |
9 |
Correct |
38 ms |
172976 KB |
Output is correct |
10 |
Correct |
43 ms |
172952 KB |
Output is correct |
11 |
Correct |
38 ms |
173020 KB |
Output is correct |
12 |
Correct |
39 ms |
172884 KB |
Output is correct |
13 |
Correct |
38 ms |
172884 KB |
Output is correct |
14 |
Correct |
42 ms |
172960 KB |
Output is correct |
15 |
Correct |
43 ms |
173148 KB |
Output is correct |
16 |
Correct |
79 ms |
177512 KB |
Output is correct |
17 |
Correct |
78 ms |
177616 KB |
Output is correct |
18 |
Correct |
80 ms |
177964 KB |
Output is correct |
19 |
Correct |
151 ms |
187460 KB |
Output is correct |
20 |
Correct |
139 ms |
187360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
172820 KB |
Output is correct |
2 |
Correct |
45 ms |
172884 KB |
Output is correct |
3 |
Correct |
39 ms |
172884 KB |
Output is correct |
4 |
Correct |
39 ms |
172792 KB |
Output is correct |
5 |
Correct |
46 ms |
172932 KB |
Output is correct |
6 |
Correct |
38 ms |
172892 KB |
Output is correct |
7 |
Correct |
40 ms |
172988 KB |
Output is correct |
8 |
Correct |
38 ms |
172896 KB |
Output is correct |
9 |
Correct |
38 ms |
172976 KB |
Output is correct |
10 |
Correct |
43 ms |
172952 KB |
Output is correct |
11 |
Correct |
38 ms |
173020 KB |
Output is correct |
12 |
Correct |
39 ms |
172884 KB |
Output is correct |
13 |
Correct |
38 ms |
172884 KB |
Output is correct |
14 |
Correct |
42 ms |
172960 KB |
Output is correct |
15 |
Correct |
43 ms |
173148 KB |
Output is correct |
16 |
Correct |
79 ms |
177512 KB |
Output is correct |
17 |
Correct |
78 ms |
177616 KB |
Output is correct |
18 |
Correct |
80 ms |
177964 KB |
Output is correct |
19 |
Correct |
151 ms |
187460 KB |
Output is correct |
20 |
Correct |
139 ms |
187360 KB |
Output is correct |
21 |
Correct |
232 ms |
188244 KB |
Output is correct |
22 |
Correct |
224 ms |
188244 KB |
Output is correct |
23 |
Correct |
228 ms |
187596 KB |
Output is correct |
24 |
Correct |
623 ms |
233648 KB |
Output is correct |
25 |
Correct |
568 ms |
233960 KB |
Output is correct |