This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n, A[200005]; map<pair<int, int>, int> swp;
int dp(int i, int j){ int l = i*2, r = i*2+1, ret = i;
if (swp.count({i, j})) return swp[{i, j}];
else if (l > n){}
else if (r > n) ret = (A[l] < j) ? l : i;
else if (A[l] < min(j, A[r])) ret = dp(l, j);
else if (A[r] < min(j, A[l])){
int mn = min(j, A[l]);
if (dp(l, mn) < dp(r, mn)) ret = ((mn == j) ? dp(l, j) : dp(r, j));
else ret = ((mn == j) ? dp(r, j) : dp(l, j));
}return swp[{i, j}] = ret;
}
int main(){
cin >> n; for (int i = 1; i <= n; i++) cin >> A[i];
for (int i = 1; i <= n; i++){ int l = i*2, r = i*2+1;
if (l > n){ continue; }
else if (r > n){ if (A[l] < A[i]) swap(A[l], A[i]); }
else if (A[l] < min(A[i], A[r])) swap(A[l], A[i]);
else if (A[r] < min(A[i], A[l])){
int mn = min(A[i], A[l]), mx = max(A[i], A[l]);
A[i] = A[r];
if (dp(l, mn) < dp(r, mn)) tie(A[l], A[r]) = {mn, mx};
else tie(A[l], A[r]) = {mx, mn};
}
}for (int i = 1; i <= n; i++) cout<<A[i]<<" ";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |