結果

問題 No.200 カードファイト!
ユーザー mamekinmamekin
提出日時 2015-07-21 23:42:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,625 bytes
コンパイル時間 1,203 ms
コンパイル使用メモリ 115,980 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 09:05:34
合計ジャッジ時間 1,950 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

class Edge{
public:
    int to, cap, cost, rev;
    Edge(){};
    Edge(int to0, int cap0, int cost0){to = to0; cap = cap0, cost = cost0;}
    Edge(int to0, int cap0, int cost0, int rev0){to = to0; cap = cap0; cost = cost0; rev = rev0;}
};

int minCostFlow(vector<vector<Edge> >& edges0, int source, int sink, int flow, bool isDetail = false)
{
    int n = edges0.size();
    vector<vector<Edge> > edges = edges0;
    for(int i=0; i<n; ++i){
        for(unsigned j=0; j<edges0[i].size(); ++j){
            const Edge& e = edges0[i][j];
            edges[i][j].rev = edges[e.to].size();
            edges[e.to].push_back(Edge(i, 0, -e.cost, j));
        }
    }

    vector<int> h(n, 0);
    vector<int> prevV(n);
    vector<int> prevE(n);

    int ret = 0;
    while(flow > 0){
        vector<int> dist(n, INT_MAX);
        dist[source] = 0;
        priority_queue<pair<int,int> ,vector<pair<int,int> >, greater<pair<int,int> > > q;
        q.push(make_pair(0, source));

        while(!q.empty()){
            pair<int, int> p = q.top();
            q.pop();
            int v = p.second;
            if(dist[v] < p.first)
                continue;
            for(unsigned i=0; i<edges[v].size(); ++i){
                Edge e = edges[v][i];
                if(e.cap > 0 && dist[v] + e.cost + h[v] - h[e.to] < dist[e.to]){
                    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                    prevV[e.to] = v;
                    prevE[e.to] = i;
                    q.push(make_pair(dist[e.to], e.to));
                }
            }
        }
        if(dist[sink] == INT_MAX){
            return -1;
        }
        for(int i=0; i<n; ++i)
            h[i] += dist[i];

        int g = flow;
        for(int i=sink; i!=source; i=prevV[i])
            g = min(g, edges[prevV[i]][prevE[i]].cap);
        for(int i=sink; i!=source; i=prevV[i]){
            Edge& e = edges[prevV[i]][prevE[i]];
            e.cap -= g;
            edges[i][e.rev].cap += g;
        }
        flow -= g;
        ret += g * h[sink];
    }

    if(isDetail){
        for(int i=0; i<n; ++i){
            for(unsigned j=0; j<edges0[i].size(); ++j){
                edges0[i][j].cap -= edges[i][j].cap;
            }
        }
    }

    return ret;
}

int main()
{
    int n, a;
    cin >> n >> 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.rbegin(), b.rend());
    sort(d.begin(), d.end());

    vector<vector<bool> > x(n, vector<bool>(n, false));
    for(int i=0; i<n; ++i)
        x[i/a][i/c] = true;

    vector<vector<Edge> > edges(2*n+2);
    int source = 2 * n;
    int sink = 2 * n + 1;
    for(int i=0; i<n; ++i){
        edges[source].push_back(Edge(i, 1, 0));
        edges[n+i].push_back(Edge(sink, 1, 0));
    }
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            if(!x[i/a][j/c])
                continue;

            if(b[i%a] <= d[j%c])
                edges[i].push_back(Edge(n+j, 1, 1));
            else
                edges[i].push_back(Edge(n+j, 1, 0));
        }
    }

    int ans = n - minCostFlow(edges, source, sink, n);
    cout << ans << endl;

    return 0;
}
0