結果

問題 No.200 カードファイト!
ユーザー しらっ亭しらっ亭
提出日時 2016-10-22 04:22:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 4,054 bytes
コンパイル時間 2,205 ms
コンパイル使用メモリ 183,324 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 09:06:06
合計ジャッジ時間 3,100 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL<<(n))
#define sz(x) ((int)(x).size())
#define fst first
#define snd second
using ll=long long;using pii=pair<int,int>;using vb=vector<bool>;
using vi=vector<int>;using vvi=vector<vi>;using vvvi=vector<vvi>;
using vl=vector<ll>;using vvl=vector<vl>;using vvvl=vector<vvl>;
using vd=vector<double>;using vvd=vector<vd>;using vvvd=vector<vvd>;
using vpii=vector<pii>;using vvpii=vector<vpii>;using vvvpii=vector<vvpii>;
template <typename T> T read() {T t; cin >> t; return t;}

struct MinCostFlow {
  struct edge { int to, cap, cost, rev; }; // 辺 (行き先、容量、コスト、逆辺)
  typedef pair<int, int> pii;
  const int INF = 1e9+9;

  int V;
  vector<vector<edge> > G;
  vector<int> h, dist, prevv, preve;

  MinCostFlow(const int V) : V(V), G(V), h(V), dist(V), prevv(V), preve(V) {};

  // fromからtoへ向かう容量cap、コストcostの辺をグラフに追加する
  void add_edge(int from, int to, int cap, int cost) {
    G[from].push_back((edge){to, cap, cost, (int) G[to].size()});
    G[to].push_back((edge){from, 0, -cost, (int) G[from].size() - 1});
  }

  // sからtへの流量fの最小費用流を求める
  // 流せない場合は-1を返す
  int min_cost_flow(int s, int t, int f) {
    int res = 0;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      // ダイクストラ法を用いてhを更新する
      priority_queue<pii, vector<pii>, greater<pii> > que;
      fill(dist.begin(), dist.end(), INF);
      dist[s] = 0;
      que.push(make_pair(0, s));
      while (!que.empty()) {
        pii p = que.top(); que.pop();
        int v = p.second;
        if (dist[v] < p.first) continue;
        for (int i = 0; i < (int) G[v].size(); i++) {
          edge &e = G[v][i];
          if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
            dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v;
            preve[e.to] = i;
            que.push(make_pair(dist[e.to], e.to));
          }
        }
      }
      if (dist[t] == INF) {
        // これ以上流せない
        return -1;
      }
      for (int v = 0; v < V; v++) h[v] += dist[v];

      // s-t間最短路に沿って目一杯流す
      int d = f;
      for (int v = t; v != s; v = prevv[v]) {
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      res += d * h[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge &e = G[prevv[v]][preve[v]]; e.cap -= d;
        G[v][e.rev].cap += d;
      }
    }
    return res;
  }
};

void Main() {
  int n = read<int>();
  MinCostFlow flow(n * 2 + 2);

  vi A, B;

  int a = read<int>();
  rep(i, a) A.push_back(read<int>());

  int b = read<int>();
  rep(i, b) B.push_back(read<int>());

  sort(all(A), greater<int>());
  sort(all(B));

  int S = n * 2;
  int T = S + 1;

  rep(i, n) flow.add_edge(S, i, 1, 0);
  rep(j, n) flow.add_edge(j + n, T, 1, 0);

  rep(i, n) {
    // A の i 番目と当たる可能性がある B の頂点にだけ辺を張っている
    int abox = i / a;

    int a_min = abox * a;
    int a_max = a_min + a - 1;

    int bbox_for_a_min = a_min / b;
    int bbox_for_a_max = a_max / b;

    int b_min = bbox_for_a_min * b;
    int b_max = bbox_for_a_max * b + b - 1;

    b_max = min(b_max, n);

    rep(j, b_min, b_max+1) {
      int c = A[i%a] > B[j%b] ? 0 : 1;
      flow.add_edge(i, j + n, 1, c);
    }
  }

  int cost = flow.min_cost_flow(S, T, n);

  cout << n - cost << endl;
}
int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }
0