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;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
const int mx = 2e5;
int a[2*mx+1];
map<ii, int> dp;
int dfs (int u, int val) {
if (dp[{u,val}]) return dp[{u,val}];
int mi = min(val, min(a[2*u], a[2*u+1]));
if (mi==a[2*u+1]) {
if (val < a[2*u]) dp[{u,val}] = min (dfs(2*u, val), dfs(2*u+1, val)) + 1;
else if (dfs(2*u, a[2*u]) <= dfs (2*u+1, a[2*u])) dp[{u,val}] = dfs (2*u+1, val) + 1;
else dp[{u,val}] = dfs (2*u, val) + 1;
}
else if (mi==a[2*u]) dp[{u,val}] = dfs (2*u, val) + 1;
else dp[{u,val}] = 1;
return dp[{u,val}];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
rep (i,1,2*mx) a[i] = 1e9;
int n; cin >> n;
rep (i,1,n) cin >> a[i];
rep (i,1,n) {
int mi = min(a[i], min(a[2*i], a[2*i+1]));
if (a[2*i+1]==mi) {
int mi2 = min(a[i], a[2*i]), ma = max(a[i], a[2*i]);
int l = dfs (2*i, mi2), r = dfs (2*i+1, mi2);
a[2*i] = mi2;
a[2*i+1] = ma;
if (l>r) swap (a[2*i], a[2*i+1]);
}
else if (a[2*i]==mi) a[2*i] = a[i];
a[i] = mi;
}
rep (i,1,n) cout << a[i] << " \n"[i==n];
return 0;
}
# | 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... |