답안 #672116

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
672116 2022-12-14T19:30:05 Z Lobo Swap (BOI16_swap) C++17
100 / 100
69 ms 35752 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 4e5+10;

int n, p[maxn], id[maxn], ans[maxn], up[maxn];
set<int> st[maxn];

void solve() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> p[i];
        st[i].insert(p[i]);
    }

    st[0].insert(n+1);
    for(int i = 1; i <= n; i++) {
        int lc = 2*i; if(lc > n) lc = 0;
        int rc = 2*i+1; if(rc > n) rc = 0;
        
        int a = inf;
        int k = i;
        while(k != 0) {
            if(st[k].size()) a = min(a, *st[k].begin());
            if(k/2 == 0 || ((up[k/2]&(1<<(k&1))) == 0)) break;
            k/= 2;
        }
        int b = *st[lc].begin();
        int c = *st[rc].begin();
        ans[i] = min({a,b,c});
        if(min({a,b,c}) == a) {
            k = i;
            while(k != 0) {
                if(st[k].count(a)) {
                    st[k].erase(a);
                    break;
                }
                up[k/2]^= (1<<(k&1));
                k/= 2;
            }

            up[i] = 0;
        }
        else if(min({a,b,c}) == b) {
            up[i] = 1;
            st[2*i].clear();
        }
        else {
            st[i].insert(*st[2*i].begin());
            up[i] = 3;
            st[2*i].clear();
            st[2*i+1].clear();
        }
    }

    for(int i = 1; i <= n; i++) {
        cout << ans[i] << " ";
    }cout << endl;

}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19116 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19100 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19116 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19100 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 10 ms 19112 KB Output is correct
7 Correct 10 ms 19028 KB Output is correct
8 Correct 10 ms 19116 KB Output is correct
9 Correct 10 ms 19040 KB Output is correct
10 Correct 11 ms 19116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19116 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19100 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 10 ms 19112 KB Output is correct
7 Correct 10 ms 19028 KB Output is correct
8 Correct 10 ms 19116 KB Output is correct
9 Correct 10 ms 19040 KB Output is correct
10 Correct 11 ms 19116 KB Output is correct
11 Correct 11 ms 19156 KB Output is correct
12 Correct 10 ms 19156 KB Output is correct
13 Correct 11 ms 19116 KB Output is correct
14 Correct 10 ms 19116 KB Output is correct
15 Correct 10 ms 19156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19116 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19100 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 10 ms 19112 KB Output is correct
7 Correct 10 ms 19028 KB Output is correct
8 Correct 10 ms 19116 KB Output is correct
9 Correct 10 ms 19040 KB Output is correct
10 Correct 11 ms 19116 KB Output is correct
11 Correct 11 ms 19156 KB Output is correct
12 Correct 10 ms 19156 KB Output is correct
13 Correct 11 ms 19116 KB Output is correct
14 Correct 10 ms 19116 KB Output is correct
15 Correct 10 ms 19156 KB Output is correct
16 Correct 22 ms 23116 KB Output is correct
17 Correct 23 ms 23088 KB Output is correct
18 Correct 25 ms 23140 KB Output is correct
19 Correct 24 ms 23224 KB Output is correct
20 Correct 24 ms 23144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 10 ms 19116 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19100 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 10 ms 19112 KB Output is correct
7 Correct 10 ms 19028 KB Output is correct
8 Correct 10 ms 19116 KB Output is correct
9 Correct 10 ms 19040 KB Output is correct
10 Correct 11 ms 19116 KB Output is correct
11 Correct 11 ms 19156 KB Output is correct
12 Correct 10 ms 19156 KB Output is correct
13 Correct 11 ms 19116 KB Output is correct
14 Correct 10 ms 19116 KB Output is correct
15 Correct 10 ms 19156 KB Output is correct
16 Correct 22 ms 23116 KB Output is correct
17 Correct 23 ms 23088 KB Output is correct
18 Correct 25 ms 23140 KB Output is correct
19 Correct 24 ms 23224 KB Output is correct
20 Correct 24 ms 23144 KB Output is correct
21 Correct 61 ms 35684 KB Output is correct
22 Correct 62 ms 35744 KB Output is correct
23 Correct 63 ms 35664 KB Output is correct
24 Correct 69 ms 35664 KB Output is correct
25 Correct 66 ms 35752 KB Output is correct