Submission #553168

# Submission time Handle Problem Language Result Execution time Memory
553168 2022-04-25T01:57:30 Z Killer2501 Swap (BOI16_swap) C++14
100 / 100
767 ms 8348 KB
#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

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 time Memory Grader output
1 Correct 3 ms 5044 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5036 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5044 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5036 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5036 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5044 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5036 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5036 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 5140 KB Output is correct
12 Correct 5 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 3 ms 5040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5044 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5036 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5036 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 5140 KB Output is correct
12 Correct 5 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 3 ms 5040 KB Output is correct
16 Correct 11 ms 5716 KB Output is correct
17 Correct 11 ms 5716 KB Output is correct
18 Correct 12 ms 5716 KB Output is correct
19 Correct 97 ms 5672 KB Output is correct
20 Correct 91 ms 5776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5044 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5036 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5036 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 5140 KB Output is correct
12 Correct 5 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 3 ms 5040 KB Output is correct
16 Correct 11 ms 5716 KB Output is correct
17 Correct 11 ms 5716 KB Output is correct
18 Correct 12 ms 5716 KB Output is correct
19 Correct 97 ms 5672 KB Output is correct
20 Correct 91 ms 5776 KB Output is correct
21 Correct 40 ms 8320 KB Output is correct
22 Correct 40 ms 8284 KB Output is correct
23 Correct 44 ms 8340 KB Output is correct
24 Correct 767 ms 8348 KB Output is correct
25 Correct 767 ms 8344 KB Output is correct