Submission #553168

#TimeUsernameProblemLanguageResultExecution timeMemory
553168Killer2501Swap (BOI16_swap)C++14
100 / 100
767 ms8348 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<int, pii>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+2;
const int M = 60;
const int mod = 1e9+7;
const ll base = 1e6;
const ll inf = 1e9;
int n, m, b[N], a[N], k, t, d[N], cnt, par[N], f[N];
ll ans;
vector<int> adj[N], vi;
bool vis[N], ok;
int dfs1(int u, int val, int h)
{
    int mn = val;
    if(u*2 <= n)mn = min(mn, a[u*2]);
    if(u*2 < n)mn = min(mn, a[u*2+1]);
    if(u*2 < n && mn == a[u*2+1])
    {
        if(a[u*2] < val)
        {
            int l = dfs1(u*2, a[u*2], h+1), r = dfs1(u*2+1, a[u*2], h+1);
            if(l <= r)return dfs1(u*2+1, val, h+1);
            return dfs1(u*2, val, h+1);
        }
        return min(dfs1(u*2, val, h+1), dfs1(u*2+1, val, h+1));
    }
    else if(u*2 <= n && mn == a[u*2])return dfs1(u*2, val, h+1);
    return h;
}
void dfs(int u)
{
    int mn = a[u];
    if(u*2 <= n)mn = min(mn, a[u*2]);
    if(u*2 < n)mn = min(mn, a[u*2+1]);
    if(u*2 < n && mn == a[u*2+1])
    {
        int mi = min(a[u], a[u*2]);
        int l = dfs1(u*2, mi, 1), r = dfs1(u*2+1, mi, 1);
        a[u*2+1] = a[u];
        if(a[u*2] > a[u*2+1])swap(a[u*2], a[u*2+1]);
        if(l > r)swap(a[u*2], a[u*2+1]);
    }
    else if(u*2 <= n && mn == a[u*2])a[u*2] = a[u];
    a[u] = mn;
    if(u*2 <= n)dfs(u*2);
    if(u*2 < n)dfs(u*2+1);
}
void sol()
{
    cin >> n;
    for(int i = 1; i <= n; i ++)cin >> a[i];
    dfs(1);
    for(int i = 1; i <= n; i ++)cout << a[i] <<" ";
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
	{
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
    int test = 1;
    //cin >> test;
    while(test -- > 0)sol();
    return 0;
}
/*
2 1 6 5 4 3
*/

Compilation message (stderr)

swap.cpp: In function 'int main()':
swap.cpp:71:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:72:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...