#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;
typedef pair<int, int> pi;
map<pi,int> bes;
int n, p[MX];
int get(int ind, int y) {
if (bes.count({ind,y})) return bes[{ind,y}];
if (2*ind > n) return ind;
if (2*ind+1 > n) {
if (y > p[2*ind]) return 2*ind;
return ind;
}
if (y < min(p[2*ind],p[2*ind+1])) return bes[{ind,y}] = ind;
if (p[2*ind] < min(y,p[2*ind+1])) return bes[{ind,y}] = get(2*ind,y);
int mn = min(y,p[2*ind]);
if (get(2*ind,mn) < get(2*ind+1,mn)) {
if (mn == y) return bes[{ind,y}] = get(2*ind,y);
return bes[{ind,y}] = get(2*ind+1,y);
} else {
if (mn == y) return bes[{ind,y}] = get(2*ind+1,y);
return bes[{ind,y}] = get(2*ind,y);
}
}
void solve(int ind) {
if (2*ind > n) return;
if (2*ind+1 > n) {
if (p[ind] > p[2*ind]) swap(p[ind],p[2*ind]);
return;
}
if (p[ind] < min(p[2*ind],p[2*ind+1])) {
} else if (p[2*ind] < min(p[ind],p[2*ind+1])) {
swap(p[2*ind],p[ind]);
} else {
int mn = min(p[ind],p[2*ind]), mx = max(p[ind],p[2*ind]);
p[ind] = p[2*ind+1];
if (get(2*ind,mn) < get(2*ind+1,mn)) {
p[2*ind] = mn, p[2*ind+1] = mx;
} else {
p[2*ind] = mx, p[2*ind+1] = mn;
}
}
solve(ind+1);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
FOR(i,1,n+1) cin >> p[i];
solve(1);
FOR(i,1,n+1) cout << p[i] << " ";
}
Compilation message
swap.cpp:7:10: error: 'MX' was not declared in this scope
7 | int n, p[MX];
| ^~
swap.cpp: In function 'int get(int, int)':
swap.cpp:13:17: error: 'p' was not declared in this scope
13 | if (y > p[2*ind]) return 2*ind;
| ^
swap.cpp:16:17: error: 'p' was not declared in this scope
16 | if (y < min(p[2*ind],p[2*ind+1])) return bes[{ind,y}] = ind;
| ^
swap.cpp:17:9: error: 'p' was not declared in this scope
17 | if (p[2*ind] < min(y,p[2*ind+1])) return bes[{ind,y}] = get(2*ind,y);
| ^
swap.cpp:18:20: error: 'p' was not declared in this scope
18 | int mn = min(y,p[2*ind]);
| ^
swap.cpp: In function 'void solve(int)':
swap.cpp:31:13: error: 'p' was not declared in this scope
31 | if (p[ind] > p[2*ind]) swap(p[ind],p[2*ind]);
| ^
swap.cpp:35:9: error: 'p' was not declared in this scope
35 | if (p[ind] < min(p[2*ind],p[2*ind+1])) {
| ^
swap.cpp:44:41: error: 'mx' was not declared in this scope; did you mean 'mn'?
44 | p[2*ind] = mn, p[2*ind+1] = mx;
| ^~
| mn
swap.cpp:46:24: error: 'mx' was not declared in this scope; did you mean 'mn'?
46 | p[2*ind] = mx, p[2*ind+1] = mn;
| ^~
| mn
swap.cpp: In function 'int main()':
swap.cpp:55:25: error: 'p' was not declared in this scope
55 | FOR(i,1,n+1) cin >> p[i];
| ^
swap.cpp:57:26: error: 'p' was not declared in this scope
57 | FOR(i,1,n+1) cout << p[i] << " ";
| ^