Submission #532446

#TimeUsernameProblemLanguageResultExecution timeMemory
532446OttoTheDinoSwap (BOI16_swap)C++17
68 / 100
159 ms58180 KiB
#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];
unordered_map<int,int> dp[mx];

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];

    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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...