結果

問題 No.200 カードファイト!
ユーザー maine_honzukimaine_honzuki
提出日時 2020-12-21 23:15:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 4,452 bytes
コンパイル時間 2,902 ms
コンパイル使用メモリ 224,000 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-15 09:02:06
合計ジャッジ時間 3,450 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 3 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 2 ms
5,248 KB
testcase_28 AC 3 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename flow_t, typename cost_t>
struct mincostflow {
private:
    int N;
    struct _edge {
        int to, rev;
        flow_t cap;
        cost_t cost;
    };
    vector<std::pair<int, int>> Pos;
    vector<std::vector<_edge>> G;

public:
    mincostflow() {}
    mincostflow(int N) : N(N), G(N) {}
    void add_edge(int from, int to, flow_t cap, cost_t cost) {
        Pos.push_back({from, int(G[from].size())});
        int from_id = int(G[from].size());
        int to_id = int(G[to].size());
        if (from == to)
            to_id++;
        G[from].push_back(_edge{to, to_id, cap, cost});
        G[to].push_back(_edge{from, from_id, 0, -cost});
    }
    pair<flow_t, cost_t> flow(int s, int t) {
        return flow(s, t, numeric_limits<flow_t>::max());
    }
    pair<flow_t, cost_t> flow(int s, int t, flow_t flow_limit) {
        return slope(s, t, flow_limit).back();
    }
    vector<pair<flow_t, cost_t>> slope(int s, int t) {
        return slope(s, t, numeric_limits<flow_t>::max());
    }
    vector<pair<flow_t, cost_t>> slope(int s, int t, flow_t flow_limit) {
        vector<cost_t> dual(N, 0), dist(N);
        vector<int> pv(N), pe(N);
        vector<bool> vis(N);
        auto dual_ref = [&]() {
            fill(dist.begin(), dist.end(), numeric_limits<cost_t>::max());
            fill(pv.begin(), pv.end(), -1);
            fill(pe.begin(), pe.end(), -1);
            fill(vis.begin(), vis.end(), false);
            struct Q {
                cost_t key;
                int to;
                bool operator<(Q r) const { return key > r.key; }
            };
            priority_queue<Q> que;
            dist[s] = 0;
            que.push(Q{0, s});
            while (!que.empty()) {
                int v = que.top().to;
                que.pop();
                if (vis[v])
                    continue;
                vis[v] = true;
                if (v == t)
                    break;
                for (int i = 0; i < int(G[v].size()); i++) {
                    auto e = G[v][i];
                    if (vis[e.to] || !e.cap)
                        continue;
                    cost_t cost = e.cost - dual[e.to] + dual[v];
                    if (dist[e.to] - dist[v] > cost) {
                        dist[e.to] = dist[v] + cost;
                        pv[e.to] = v;
                        pe[e.to] = i;
                        que.push(Q{dist[e.to], e.to});
                    }
                }
            }
            if (!vis[t])
                return false;
            for (int v = 0; v < N; v++) {
                if (!vis[v])
                    continue;
                dual[v] -= dist[t] - dist[v];
            }
            return true;
        };
        flow_t flow = 0;
        cost_t cost = 0, prev_cost_per_flow = -1;
        vector<pair<flow_t, cost_t>> result;
        result.push_back({flow, cost});
        while (flow < flow_limit) {
            if (!dual_ref())
                break;
            flow_t c = flow_limit - flow;
            for (int v = t; v != s; v = pv[v])
                c = min(c, G[pv[v]][pe[v]].cap);
            for (int v = t; v != s; v = pv[v]) {
                auto& e = G[pv[v]][pe[v]];
                e.cap -= c;
                G[v][e.rev].cap += c;
            }
            cost_t d = -dual[s];
            flow += c;
            cost += c * d;
            if (prev_cost_per_flow == d)
                result.pop_back();
            result.push_back({flow, cost});
            prev_cost_per_flow = d;
        }
        return result;
    }
};

int main() {
    int N, A, C;
    cin >> N >> A;
    vector<int> B(A);
    for (int i = 0; i < A; i++) {
        cin >> B[i];
    }
    cin >> C;
    vector<int> D(C);
    for (int i = 0; i < C; i++) {
        cin >> D[i];
    }

    sort(B.begin(), B.end(), greater<>());
    sort(D.begin(), D.end());

    mincostflow<int, int> mcf(N * 2 + 2);
    int s = N * 2, t = N * 2 + 1;
    for (int i = 0; i < N; i++) {
        mcf.add_edge(s, i, 1, 0);
        mcf.add_edge(N + i, t, 1, 0);
    }

    for (int i = 0; i < N; i++) {
        int l = i / A * A;
        int r = min(l + A, N);
        l = l / C * C;
        r = min(r / C * C + C, N);
        for (int j = l; j < r; j++) {
            int cost = (B[i % A] <= D[j % C]);
            mcf.add_edge(i, N + j, 1, cost);
        }
    }

    cout << N - mcf.flow(s, t).second << endl;
}
0