답안 #614111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
614111 2022-07-30T18:58:08 Z skittles1412 버섯 세기 (IOI20_mushrooms) C++17
100 / 100
520 ms 420 KB
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__);
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
    for (int i = 0; i < sz(arr); i++) {
        if (i) {
            out << " ";
        }
        out << arr[i];
    }
    return out;
}

int use_machine(std::vector<int> x);

const int bsize = 100;

template <typename T>
struct DT {
    vector<int> query;
    vector<T> res;

    operator bool() const {
        return sz(query);
    }
};

template <typename U, typename T>
U feach(int n, int x, int y, const T& t) {
    vector<int> perm(n);
    iota(begin(perm), end(perm), 2);
    perm.insert(perm.end(), x, 0);
    perm.insert(perm.end(), y, 1);
    do {
        U cur = t(perm);
        if (cur) {
            return cur;
        }
    } while (next_permutation(begin(perm), end(perm)));
    return U {};
}

int eval(int mask, const vector<int>& perm) {
    auto get = [&](int ind) -> int {
        if (perm[ind] >= 2) {
            return (mask >> (perm[ind] - 2)) & 1;
        }
        return perm[ind];
    };
    int ans = 0;
    for (int i = 0; i < sz(perm) - 1; i++) {
        ans += get(i) != get(i + 1);
    }
    return ans;
}

// n unknowns, x zeroes, y ones
DT<DT<int>> compute_dt(int n, int x, int y) {
    int tot = n + x + y;
    return feach<DT<DT<int>>>(n, x, y, [&](const vector<int>& perm) {
        vector<int> res[tot];
        for (int i = 0; i < (1 << n); i++) {
            res[eval(i, perm)].push_back(i);
        }
        DT<DT<int>> ans;
        ans.query = perm;
        for (auto& a : res) {
            DT<int> cur = feach<DT<int>>(n, x, y, [&](const vector<int>& perm) {
                DT<int> ans2;
                ans2.query = perm;
                ans2.res.resize(tot, -1);
                for (auto& b : a) {
                    int cur = eval(b, perm);
                    if (ans2.res[cur] != -1) {
                        return DT<int> {};
                    }
                    ans2.res[cur] = b;
                }
                return ans2;
            });
            if (!cur) {
                return DT<DT<int>> {};
            }
            ans.res.push_back(cur);
        }
        return ans;
    });
}

struct State {
    int i = 1, ans = 0, n;
    vector<int> inds[2];
    map<array<int, 3>, DT<DT<int>>> cache;

    State(int n) : n(n), inds {{0}, {}} {}

    int query(const vector<int>& arr, const vector<int>& vars) {
        int cind[2] {};
        vector<int> cur;
        for (auto& a : arr) {
            if (a < 2) {
                cur.push_back(inds[a][cind[a]++]);
            } else {
                cur.push_back(vars[a - 2]);
            }
        }
        dbg(arr, cur);
        return use_machine(cur);
    }

    void eval(int n, int x, int y) {
        n = min(n, this->n - i);
        auto [it, inserted] = cache.insert({{n, x, y}, {}});
        if (inserted) {
            it->second = compute_dt(n, x, y);
        }
        vector<int> vars;
        for (int i = 0; i < n; i++) {
            vars.push_back(this->i++);
        }
        eval(it->second, vars);
    }

    void eval(const DT<DT<int>>& dt, const vector<int>& vars) {
        eval(dt.res[query(dt.query, vars)], vars);
    }

    void eval(const DT<int>& dt, const vector<int>& vars) {
        vector<int> poss;
        for (int i = 0; i < sz(dt.res); i++) {
            if (dt.res[i] != -1) {
                poss.push_back(dt.res[i]);
            }
        }
        int ans;
        if (sz(poss) == 1) {
            ans = poss[0];
        } else {
            ans = dt.res[query(dt.query, vars)];
        }
        for (int i = 0; i < sz(vars); i++) {
            inds[(ans >> i) & 1].push_back(vars[i]);
        }
    }

    void eval_opt() {
        int a = sz(inds[0]), b = sz(inds[1]);
        if (a == 1 && b == 0) {
            eval(1, 1, 0);
        } else if (a + b == 2) {
            eval(2, a, b);
        } else {
            assert(a + b >= 4);
            if (a && b) {
                int ax = min(a, 3), bx = 4 - ax;
                assert(ax <= a && bx <= b);
                eval(5, ax, bx);
            } else {
                assert(a >= 4 && !b);
                eval(4, 4, 0);
            }
        }
    }

    void query_mult() {
        int cind;
        if (sz(inds[0]) >= sz(inds[1])) {
            cind = 0;
        } else {
            cind = 1;
        }
        vector<int> cur;
        for (int j = 0; i < n && j < sz(inds[cind]); i++, j++) {
            cur.push_back(inds[cind][j]);
            cur.push_back(i);
        }
        int cans = use_machine(cur), x = (cans + 1) / 2;
        if (!cind) {
            ans += sz(cur) / 2 - x;
        } else {
            ans += x;
        }
        if (cans & 1) {
            inds[cind ^ 1].push_back(cur.back());
        } else {
            inds[cind].push_back(cur.back());
        }
    }

    int solve() {
        ans = sz(inds[0]);
        while (i < n) {
            query_mult();
        }
        return ans;
    }
};

int count_mushrooms(int n) {
    State state(n);
    while (state.i < n && state.i < 2 * bsize - 1) {
        state.eval_opt();
        dbg(sz(state.inds[0]), sz(state.inds[1]));
    }
    return state.solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 258 ms 208 KB Output is correct
4 Correct 431 ms 292 KB Output is correct
5 Correct 406 ms 208 KB Output is correct
6 Correct 401 ms 296 KB Output is correct
7 Correct 5 ms 208 KB Output is correct
8 Correct 162 ms 304 KB Output is correct
9 Correct 160 ms 420 KB Output is correct
10 Correct 432 ms 288 KB Output is correct
11 Correct 169 ms 208 KB Output is correct
12 Correct 180 ms 288 KB Output is correct
13 Correct 515 ms 300 KB Output is correct
14 Correct 161 ms 292 KB Output is correct
15 Correct 423 ms 308 KB Output is correct
16 Correct 171 ms 288 KB Output is correct
17 Correct 400 ms 312 KB Output is correct
18 Correct 520 ms 292 KB Output is correct
19 Correct 167 ms 308 KB Output is correct
20 Correct 193 ms 296 KB Output is correct
21 Correct 275 ms 300 KB Output is correct
22 Correct 284 ms 292 KB Output is correct
23 Correct 407 ms 296 KB Output is correct
24 Correct 408 ms 304 KB Output is correct
25 Correct 425 ms 292 KB Output is correct
26 Correct 283 ms 292 KB Output is correct
27 Correct 285 ms 296 KB Output is correct
28 Correct 292 ms 308 KB Output is correct
29 Correct 416 ms 288 KB Output is correct
30 Correct 410 ms 292 KB Output is correct
31 Correct 399 ms 296 KB Output is correct
32 Correct 410 ms 288 KB Output is correct
33 Correct 396 ms 288 KB Output is correct
34 Correct 398 ms 300 KB Output is correct
35 Correct 406 ms 292 KB Output is correct
36 Correct 398 ms 292 KB Output is correct
37 Correct 394 ms 304 KB Output is correct
38 Correct 404 ms 300 KB Output is correct
39 Correct 402 ms 316 KB Output is correct
40 Correct 405 ms 300 KB Output is correct
41 Correct 398 ms 208 KB Output is correct
42 Correct 442 ms 300 KB Output is correct
43 Correct 443 ms 320 KB Output is correct
44 Correct 398 ms 288 KB Output is correct
45 Correct 410 ms 292 KB Output is correct
46 Correct 445 ms 292 KB Output is correct
47 Correct 410 ms 288 KB Output is correct
48 Correct 413 ms 292 KB Output is correct
49 Correct 399 ms 288 KB Output is correct
50 Correct 440 ms 208 KB Output is correct
51 Correct 404 ms 288 KB Output is correct
52 Correct 400 ms 288 KB Output is correct
53 Correct 409 ms 304 KB Output is correct
54 Correct 391 ms 292 KB Output is correct
55 Correct 416 ms 288 KB Output is correct
56 Correct 399 ms 288 KB Output is correct
57 Correct 427 ms 312 KB Output is correct
58 Correct 419 ms 312 KB Output is correct
59 Correct 403 ms 288 KB Output is correct
60 Correct 403 ms 308 KB Output is correct
61 Correct 399 ms 300 KB Output is correct
62 Correct 0 ms 208 KB Output is correct
63 Correct 0 ms 208 KB Output is correct
64 Correct 0 ms 208 KB Output is correct
65 Correct 1 ms 208 KB Output is correct
66 Correct 1 ms 208 KB Output is correct
67 Correct 0 ms 208 KB Output is correct
68 Correct 0 ms 208 KB Output is correct
69 Correct 1 ms 208 KB Output is correct
70 Correct 1 ms 208 KB Output is correct
71 Correct 1 ms 208 KB Output is correct
72 Correct 1 ms 208 KB Output is correct
73 Correct 0 ms 208 KB Output is correct
74 Correct 0 ms 208 KB Output is correct
75 Correct 1 ms 208 KB Output is correct
76 Correct 1 ms 208 KB Output is correct
77 Correct 1 ms 208 KB Output is correct
78 Correct 1 ms 208 KB Output is correct
79 Correct 1 ms 208 KB Output is correct
80 Correct 0 ms 208 KB Output is correct
81 Correct 1 ms 208 KB Output is correct
82 Correct 1 ms 208 KB Output is correct
83 Correct 1 ms 208 KB Output is correct
84 Correct 1 ms 208 KB Output is correct
85 Correct 1 ms 208 KB Output is correct
86 Correct 1 ms 308 KB Output is correct
87 Correct 1 ms 208 KB Output is correct
88 Correct 1 ms 208 KB Output is correct
89 Correct 1 ms 208 KB Output is correct
90 Correct 0 ms 208 KB Output is correct
91 Correct 0 ms 208 KB Output is correct