답안 #396481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396481 2021-04-30T03:20:33 Z KoD Olympiads (BOI19_olympiads) C++17
100 / 100
19 ms 3444 KB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

template <class F>
struct RecLambda: private F {
    explicit RecLambda(F&& f): F(std::forward<F>(f)) { }
    template <class... Args>
    decltype(auto) operator () (Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

struct State {
    int sum, alive;
    Vec<int> idx;
    Vec<bool> use;
    bool operator < (const State& other) const {
        return sum < other.sum;
    }
};

int main() {
    int N, K;
    long long C;
    std::cin >> N >> K >> C;
    Vec<Vec<int>> get(N, Vec<int>(K));
    for (auto& v: get) {
        for (auto& x: v) {
            std::cin >> x;
        }
    }
    Vec<Vec<int>> order(K, Vec<int>(N));
    for (int i = 0; i < K; ++i) {
        auto& vec = order[i];
        std::iota(vec.begin(), vec.end(), 0);
        std::stable_sort(vec.begin(), vec.end(), [&](const int x, const int y) {
            return get[x][i] > get[y][i];
        });
    }
    Vec<Vec<long long>> binom(N + 1, Vec<long long>(K + 1));
    binom[0][0] = 1;
    for (int i = 1; i <= N; ++i) {
        binom[i][0] = 1;
        for (int j = 1; j <= K; ++j) {
            binom[i][j] = binom[i - 1][j] + binom[i - 1][j - 1];
        }
    }
    const auto calc = [&](State& state) {
        state.sum = 0;
        for (int i = 0; i < K; ++i) {
            state.sum += get[order[i][state.idx[i]]][i];
        }
    };
    std::priority_queue<State> que;
    std::set<Vec<int>> seen;
    const auto push = [&](State&& state) {
        if (seen.find(state.idx) == seen.end()) {
            seen.insert(state.idx);
            calc(state);
            que.push(std::move(state));
        }
    };
    State initial {
        0, N,
        Vec<int>(K, 0),
        Vec<bool>(N, true),
    };
    push(std::move(initial));
    while (!que.empty()) {
        const auto state = que.top();
        que.pop();
        bool ok = true;
        std::set<int> set;
        for (int i = 0; i < K; ++i) {
            if (!state.use[order[i][state.idx[i]]]) {
                ok = false;
                break;
            }
            set.insert(order[i][state.idx[i]]);
        }
        if (ok) {
            C -= binom[state.alive - (int) set.size()][K - (int) set.size()];
            if (C <= 0) {
                std::cout << state.sum << '\n';
                return 0;
            }
        }
        for (int i = 0; i < K; ++i) {
            State next = state;
            auto& idx = next.idx[i];
            if (next.use[order[i][idx]]) {
                next.use[order[i][idx]] = false;
                next.alive -= 1;
            }
            while (idx < N and !next.use[order[i][idx]]) {
                idx += 1;
            }
            if (idx < N) {
                push(std::move(next));
            }
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 5 ms 1080 KB Output is correct
3 Correct 9 ms 1592 KB Output is correct
4 Correct 9 ms 1592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 13 ms 2524 KB Output is correct
4 Correct 14 ms 3320 KB Output is correct
5 Correct 19 ms 3444 KB Output is correct
6 Correct 16 ms 2940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 5 ms 1080 KB Output is correct
7 Correct 9 ms 1592 KB Output is correct
8 Correct 9 ms 1592 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 13 ms 2524 KB Output is correct
12 Correct 14 ms 3320 KB Output is correct
13 Correct 19 ms 3444 KB Output is correct
14 Correct 16 ms 2940 KB Output is correct
15 Correct 12 ms 1572 KB Output is correct
16 Correct 6 ms 1100 KB Output is correct
17 Correct 15 ms 3348 KB Output is correct
18 Correct 14 ms 2712 KB Output is correct
19 Correct 3 ms 332 KB Output is correct