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;
const int N = 200005;
const int INF = 1e9;
int n, a[N];
void solve(int v) {
int lef = (v << 1);
int rig = (v << 1 | 1);
if (rig > n) {
if (a[lef] < a[v]) {
swap(a[lef], a[v]);
}
return;
}
int minall = min({a[v], a[lef], a[rig]});
if (minall == a[v]) {
return;
}
else if (minall == a[lef]) {
swap(a[lef], a[v]); return;
}
else { // minall = a[rig]
// -> a[rig], a[v], a[lef]
// -> a[rig], a[lef], a[v]
int val[2] = {a[v], a[lef]};
a[v] = a[rig];
vector <int> best(4, INF + 2);
int best_t = 0;
for (int t = 0; t < 2; ++t) {
// a[lef] = val[t], a[rig] = val[t ^ 1]
int curlef = val[t];
int currig = val[t ^ 1];
int childlef = min( (lef * 2 <= n) ? a[lef * 2] : INF, (lef * 2 + 1 <= n) ? a[lef * 2 + 1] : INF );
int childrig = min( (rig * 2 <= n) ? a[rig * 2] : INF + 1, (rig * 2 + 1 <= n) ? a[rig * 2 + 1] : INF + 1 );
if (curlef > childlef) swap(curlef, childlef);
if (currig > childrig) swap(currig, childrig);
vector <int> cur;
cur.push_back(curlef);
cur.push_back(currig);
cur.push_back(childlef);
cur.push_back(childrig);
if (cur < best) {
best_t = t;
best = cur;
}
}
a[lef] = val[best_t];
a[rig] = val[best_t ^ 1];
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= (n / 2); ++i) {
solve(i);
}
for (int i = 1; i <= n; ++i) {
printf("%d ", a[i]);
}
printf("\n");
}
# | 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... |