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 main() {
ios::sync_with_stdio(0);
cin.tie(0);
int a[2*mx+1];
map<ii, int> dp;
rep (i,1,2*mx) a[i] = 1e9;
int n; cin >> n;
rep (i,1,n) cin >> a[i];
rrep (i,n,1) {
vi aff;
aff.pb(a[i]);
int p = i/2;
while (p) {
if (aff.back()==a[2*p+1]) aff.pb(a[2*p]);
aff.pb(a[p]);
p = p/2;
}
for (int x : aff) {
int mi = min(x, min(a[i*2], a[2*i+1]));
if (x==mi) dp[{i,x}] = 1;
else if (a[i*2]==mi) dp[{i,x}] = dp[{2*i,x}]+1;
else {
if (a[2*i]<x) {
if (dp[{2*i, a[2*i]}] <= dp[{2*i+1, a[2*i]}]) dp[{i,x}] = dp[{2*i+1,x}]+1;
else dp[{i,x}] = dp[{2*i,x}]+1;
}
else dp[{i,x}] = min(dp[{2*i,x}], dp[{2*i+1,x}])+1;
}
}
}
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]);
a[2*i] = mi2;
a[2*i+1] = ma;
if (dp[{2*i,mi2}]>dp[{2*i+1,mi2}]) 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... |