#include <bits/stdc++.h>
#define ll long long
#define ok puts("ok");
using namespace std;
const int N = (int)2e5 + 7;
int n;
int a[N];
map <pair<int, int>, int> mp;
int get(int ind, int y) {
if (mp.count({ind, y})) return mp[{ind, y}];
if (ind * 2 > n) return ind;
if (ind * 2 + 1 > n) {
if (y > a[ind * 2]) {
return ind * 2;
}
return ind;
}
if (y < min(a[ind * 2], a[ind * 2 + 1])) {
return ind;
} else if (a[ind * 2] < min(y, a[ind * 2 + 1])) {
return mp[{ind, y}] = get(ind * 2, y);
} else {
int mn = min(y, a[ind * 2]);
int mx = max(y, a[ind * 2]);
if (get(ind * 2, mn) < get(ind * 2 + 1, mn)) {
if (y == mn) return mp[{ind, y}] = get(ind * 2, y);
return mp[{ind, y}] = get(ind * 2 + 1, y);
} else {
if (y == mn) return mp[{ind, y}] = get(ind * 2 + 1, y);
return mp[{ind, y}] = get(ind * 2, y);
}
}
}
void solve(int ind) {
if (ind * 2 > n) return ;
if (ind * 2 + 1 > n) {
if (a[ind] > a[ind * 2]) {
swap(a[ind], a[ind * 2]);
}
return ;
}
if (a[ind] < min(a[ind * 2], a[ind * 2 + 1])) {
} else if (a[ind * 2] < min(a[ind], a[ind * 2 + 1])) {
swap(a[ind * 2], a[ind]);
} else {
int mn = min(a[ind], a[ind * 2]);
int mx = max(a[ind], a[ind * 2]);
a[ind] = a[ind * 2 + 1];
if (get(ind * 2, mn) < get(ind * 2 + 1, mn)) {
a[ind * 2] = mn;
a[ind * 2 + 1] = mx;
} else {
a[ind * 2] = mx;
a[ind * 2 + 1] = mn;
}
}
solve(ind + 1);
}
main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
solve(1);
for (int i = 1; i <= n; i++) {
printf("%d ", a[i]);
}
}
Compilation message
swap.cpp: In function 'int get(int, int)':
swap.cpp:29:7: warning: unused variable 'mx' [-Wunused-variable]
int mx = max(y, a[ind * 2]);
^~
swap.cpp: At global scope:
swap.cpp:67:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
swap.cpp: In function 'int main()':
swap.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
swap.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
356 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
356 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
356 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
18 ms |
2048 KB |
Output is correct |
17 |
Correct |
18 ms |
2048 KB |
Output is correct |
18 |
Correct |
18 ms |
2020 KB |
Output is correct |
19 |
Correct |
42 ms |
4208 KB |
Output is correct |
20 |
Correct |
74 ms |
4244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
356 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
18 ms |
2048 KB |
Output is correct |
17 |
Correct |
18 ms |
2048 KB |
Output is correct |
18 |
Correct |
18 ms |
2020 KB |
Output is correct |
19 |
Correct |
42 ms |
4208 KB |
Output is correct |
20 |
Correct |
74 ms |
4244 KB |
Output is correct |
21 |
Correct |
73 ms |
7416 KB |
Output is correct |
22 |
Correct |
73 ms |
7368 KB |
Output is correct |
23 |
Correct |
72 ms |
7492 KB |
Output is correct |
24 |
Correct |
202 ms |
16072 KB |
Output is correct |
25 |
Correct |
198 ms |
16168 KB |
Output is correct |