답안 #735904

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
735904 2023-05-05T02:00:36 Z baokhue232005 Swap (BOI16_swap) C++14
100 / 100
100 ms 35148 KB
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
// lethal option

#include<bits/stdc++.h>
using namespace std;

#define all(flg) flg.begin(), flg.end()
#define pb push_back
#define fi first
#define se second
#define eb emplace_back
#define ii pair<int, int>
#define vi vector<int>
#define PI 3.141592653589793238462643383279502884
#define ll long long
#define ld long double
#define for1(i, ff, gg) for(int i = ff; i <= gg; ++i)
#define for2(i, ff, gg) for(int i = ff; i >= gg; --i)
const int mod = 1e9 + 7;
const int maxN = 1e6 + 5;
int n, a[maxN];
int x, y, z, k;


int cnt = n;
vi vc[maxN];

bool used[maxN];
int calc(int node, int dep = 0){
    if(dep > 100) exit(0);
    if(node <= n){
        if(used[node]) return mod;
        else return node;
    }
    int res = mod;
    for(int cc : vc[node]){
        int val = calc(cc, dep + 1);
        res = min(res, val);
    }
    return res;
}

bool cutt(int node, int vl){
    if(node <= n){
        if(node == vl){
            assert(used[node] == 0);
            used[node] = 1;
            return 1;
        }
        return 0;
    }
    for1(i, 0, (int)vc[node].size() - 1){
        if(cutt(vc[node][i], vl)){
            vc[node].erase(vc[node].begin() + i);
            return 1;
        }
    }
    return 0;
}

int ans[maxN];

void asn(int plc){
    int vl = calc(a[plc]);
    ans[plc] = vl;
    assert(cutt(a[plc], vl));
}

signed main(){
    // freopen(".inp", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    memset(used, 0, sizeof(used));
    memset(a, 0, sizeof(a));
    for1(i, 1, n) cin >> a[i];
    cnt = n;
    used[0] = 1;
    for1(i, 1, n){
        int vl = calc(a[i]);
        int left = i * 2;
        int right = i * 2 + 1;
        int semi = min(calc(a[right]), calc(a[left]));
        if(semi >= vl){
            asn(i);
            continue;
        }
        if(calc(a[left]) < calc(a[right])){
            swap(a[i], a[left]);
            asn(i);
            continue;
        }
        int node = ++cnt;
        vc[node].pb(a[left]);
        vc[node].pb(a[i]);
        a[i] = a[right];
        asn(i);
        a[left] = node;
        a[right] = node;
    }
    for1(i, 1, n) cout << ans[i] << " ";
    cout << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 16 ms 28680 KB Output is correct
3 Correct 13 ms 28704 KB Output is correct
4 Correct 13 ms 28680 KB Output is correct
5 Correct 13 ms 28628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 16 ms 28680 KB Output is correct
3 Correct 13 ms 28704 KB Output is correct
4 Correct 13 ms 28680 KB Output is correct
5 Correct 13 ms 28628 KB Output is correct
6 Correct 16 ms 28676 KB Output is correct
7 Correct 13 ms 28676 KB Output is correct
8 Correct 14 ms 28628 KB Output is correct
9 Correct 13 ms 28612 KB Output is correct
10 Correct 13 ms 28596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 16 ms 28680 KB Output is correct
3 Correct 13 ms 28704 KB Output is correct
4 Correct 13 ms 28680 KB Output is correct
5 Correct 13 ms 28628 KB Output is correct
6 Correct 16 ms 28676 KB Output is correct
7 Correct 13 ms 28676 KB Output is correct
8 Correct 14 ms 28628 KB Output is correct
9 Correct 13 ms 28612 KB Output is correct
10 Correct 13 ms 28596 KB Output is correct
11 Correct 13 ms 28680 KB Output is correct
12 Correct 14 ms 28680 KB Output is correct
13 Correct 14 ms 28676 KB Output is correct
14 Correct 15 ms 28628 KB Output is correct
15 Correct 14 ms 28680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 16 ms 28680 KB Output is correct
3 Correct 13 ms 28704 KB Output is correct
4 Correct 13 ms 28680 KB Output is correct
5 Correct 13 ms 28628 KB Output is correct
6 Correct 16 ms 28676 KB Output is correct
7 Correct 13 ms 28676 KB Output is correct
8 Correct 14 ms 28628 KB Output is correct
9 Correct 13 ms 28612 KB Output is correct
10 Correct 13 ms 28596 KB Output is correct
11 Correct 13 ms 28680 KB Output is correct
12 Correct 14 ms 28680 KB Output is correct
13 Correct 14 ms 28676 KB Output is correct
14 Correct 15 ms 28628 KB Output is correct
15 Correct 14 ms 28680 KB Output is correct
16 Correct 25 ms 29708 KB Output is correct
17 Correct 24 ms 29788 KB Output is correct
18 Correct 25 ms 29708 KB Output is correct
19 Correct 36 ms 30168 KB Output is correct
20 Correct 34 ms 30220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 16 ms 28680 KB Output is correct
3 Correct 13 ms 28704 KB Output is correct
4 Correct 13 ms 28680 KB Output is correct
5 Correct 13 ms 28628 KB Output is correct
6 Correct 16 ms 28676 KB Output is correct
7 Correct 13 ms 28676 KB Output is correct
8 Correct 14 ms 28628 KB Output is correct
9 Correct 13 ms 28612 KB Output is correct
10 Correct 13 ms 28596 KB Output is correct
11 Correct 13 ms 28680 KB Output is correct
12 Correct 14 ms 28680 KB Output is correct
13 Correct 14 ms 28676 KB Output is correct
14 Correct 15 ms 28628 KB Output is correct
15 Correct 14 ms 28680 KB Output is correct
16 Correct 25 ms 29708 KB Output is correct
17 Correct 24 ms 29788 KB Output is correct
18 Correct 25 ms 29708 KB Output is correct
19 Correct 36 ms 30168 KB Output is correct
20 Correct 34 ms 30220 KB Output is correct
21 Correct 56 ms 33276 KB Output is correct
22 Correct 57 ms 33280 KB Output is correct
23 Correct 59 ms 33284 KB Output is correct
24 Correct 100 ms 35148 KB Output is correct
25 Correct 99 ms 35072 KB Output is correct