답안 #724075

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
724075 2023-04-14T16:52:42 Z _martynas Swap (BOI16_swap) C++14
100 / 100
167 ms 33388 KB
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back

struct Decision {
    Decision *def = nullptr, *oth = nullptr; // default, other
    bool locked = false; // locked state is shared between swappable pairs
    int val = 0; // leaf nodes aren't actual decisions and have >0 values
    Decision *link = nullptr; // linked decision
    Decision() {};
    Decision(int x) {
        // leaf node constructor
        val = x;
    }
    Decision(Decision* _def, Decision* _oth) {
        def = _def, oth = _oth;
    }
    int get() {
        if(val) return (locked ? 1e8 : val);
        if(locked) return def->get(); // other is not an option
        return min(def->get(), oth->get());
    }
    bool rem(int x) {
        // lock all decisions that lead to the one at the top of the stack
        // get()'ing x
        if(val == x) {
            locked = true;
            return true;
        }
        if(val) return false;
        bool found = def->rem(x);
        if(found) {
            locked = true;
            return true;
        }
        found = oth->rem(x);
        if(found) {
            locked = true;
            swap(def, oth);
            link->locked = true;
            swap(link->def, link->oth);
            return true;
        }
        return false;
    }
};

const int MXN = 200'005;

int n, A[MXN];
Decision* D[MXN];

int main() {
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> A[i];
    for(int i = 1; i <= n; i++) {
        D[i] = new Decision(A[i]);
    }
    for(int i = 2; i <= n; i++) {
        int j = i/2;
        tie(D[j], D[i]) = make_tuple(new Decision(D[j], D[i]), new Decision(D[i], D[j]));
        D[j]->link = D[i], D[i]->link = D[j];
    }
    for(int i = 1; i <= n; i++) {
        int x = D[i]->get();
        D[i]->rem(x);
        cout << x << " \n"[i == n];
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 436 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 436 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 28 ms 8384 KB Output is correct
17 Correct 28 ms 8484 KB Output is correct
18 Correct 28 ms 8472 KB Output is correct
19 Correct 34 ms 8508 KB Output is correct
20 Correct 31 ms 8404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 436 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 28 ms 8384 KB Output is correct
17 Correct 28 ms 8484 KB Output is correct
18 Correct 28 ms 8472 KB Output is correct
19 Correct 34 ms 8508 KB Output is correct
20 Correct 31 ms 8404 KB Output is correct
21 Correct 124 ms 33320 KB Output is correct
22 Correct 121 ms 33340 KB Output is correct
23 Correct 132 ms 33376 KB Output is correct
24 Correct 167 ms 33388 KB Output is correct
25 Correct 141 ms 33284 KB Output is correct