結果

問題 No.200 カードファイト!
ユーザー simansiman
提出日時 2022-03-25 19:27:29
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 3,638 bytes
コンパイル時間 3,835 ms
コンパイル使用メモリ 146,936 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-10-15 09:02:16
合計ジャッジ時間 2,986 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_V = 200010;
const ll INF = LLONG_MAX;

typedef pair<ll, int> P;
int V;

struct Edge {
  int to;
  ll cap;
  ll cost;
  int rev;

  Edge(int to = -1, ll cap = -1, ll cost = -1, int rev = -1) {
    this->to = to;
    this->cap = cap;
    this->cost = cost;
    this->rev = rev;
  }
};

ll h[MAX_V];
ll dist[MAX_V];
int prevv[MAX_V];
int preve[MAX_V];
vector <Edge> G[MAX_V];

class MinCostFlow {
public:
  int V;

  MinCostFlow(int V) {
    this->V = V;
  }

  void add_edge(int from, int to, ll cap, ll cost) {
    G[from].push_back(Edge(to, cap, cost, G[to].size()));
    G[to].push_back(Edge(from, 0, -cost, G[from].size() - 1));
  }

  ll min_cost_flow(int s, int t, ll flow_limit) {
    ll f = 0;
    ll totalCost = 0;
    fill(h, h + MAX_V, 0);

    while (f < flow_limit) {
      // fprintf(stderr, "f: %lld, limit: %lld\n", f, flow_limit);
      priority_queue<P, vector<P>, greater<P>> pque;
      fill(dist, dist + V, INF);
      dist[s] = 0;
      pque.push(P(0, s));

      while (!pque.empty()) {
        P p = pque.top();
        pque.pop();
        int v = p.second;
        if (dist[v] < p.first) continue;

        for (int i = 0; i < (int) G[v].size(); ++i) {
          Edge *edge = &G[v][i];
          if (edge->cap <= 0) continue;

          ll cost = edge->cost + h[v] - h[edge->to];
          if (dist[edge->to] - dist[v] > cost) {
            dist[edge->to] = dist[v] + cost;
            prevv[edge->to] = v;
            preve[edge->to] = i;
            pque.push(P(dist[edge->to], edge->to));
          }
        }
      }

      if (dist[t] == INF) {
        return -1;
      }

      for (int v = 0; v < V; ++v) {
        h[v] += dist[v];
      }

      ll c = flow_limit - f;
      for (int v = t; v != s; v = prevv[v]) {
        c = min(c, G[prevv[v]][preve[v]].cap);
      }

      f += c;
      totalCost += c * h[t];

      // fprintf(stderr, "h[s]: %d, h[t]: %d, cost: %d, prev_cost: %d\n", h[s], h[t], cost, prev_cost);

      for (int v = t; v != s; v = prevv[v]) {
        Edge *edge = &G[prevv[v]][preve[v]];
        edge->cap -= c;
        G[v][edge->rev].cap += c;
      }
    }

    return totalCost;
  }
};

int main() {
  int N;
  cin >> N;
  int A;
  cin >> A;
  vector<int> B(A);

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

  sort(B.begin(), B.end(), greater<int>());
  sort(D.begin(), D.end());
  V = 2 * N + 2;

  MinCostFlow mcf(V);
  int s = 2 * N;
  int t = 2 * N + 1;

  vector<int> cards_A;
  vector<int> L_B;
  vector<int> cards_C;
  vector<int> L_D;
  for (int i = 0; i < N; ++i) {
    int b = B[i % A];
    int d = D[i % C];
    cards_A.push_back(b);
    cards_C.push_back(d);

    L_B.push_back(i / A);
    L_D.push_back(i / C);

    mcf.add_edge(s, i, 1, 0);
    mcf.add_edge(i + N, t, 1, 0);
  }
  bool can_battle[N + 1][N + 1];
  memset(can_battle, false, sizeof(can_battle));

  for (int i = 0; i < N; ++i) {
    can_battle[L_B[i]][L_D[i]] = true;
  }

  for (int i = 0; i < N; ++i) {
    int a = cards_A[i];
    for (int j = 0; j < N; ++j) {
      int b = cards_C[j];
      if (not can_battle[L_B[i]][L_D[j]]) continue;

      if (a > b) {
        mcf.add_edge(i, j + N, 1, 0);
      } else {
        mcf.add_edge(i, j + N, 1, 1);
      }
    }
  }

  cout << N - mcf.min_cost_flow(s, t, N) << endl;

  return 0;
}

0