結果

問題 No.200 カードファイト!
ユーザー heno239heno239
提出日時 2022-09-24 12:37:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,265 bytes
コンパイル時間 1,505 ms
コンパイル使用メモリ 138,284 KB
実行使用メモリ 6,172 KB
最終ジャッジ日時 2023-08-23 21:22:07
合計ジャッジ時間 3,646 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,924 KB
testcase_01 WA -
testcase_02 AC 3 ms
5,720 KB
testcase_03 AC 3 ms
5,712 KB
testcase_04 AC 3 ms
5,908 KB
testcase_05 AC 4 ms
5,884 KB
testcase_06 AC 5 ms
5,916 KB
testcase_07 AC 5 ms
6,060 KB
testcase_08 AC 5 ms
5,868 KB
testcase_09 AC 5 ms
5,908 KB
testcase_10 AC 3 ms
5,756 KB
testcase_11 AC 3 ms
5,808 KB
testcase_12 AC 4 ms
5,952 KB
testcase_13 AC 3 ms
5,788 KB
testcase_14 AC 3 ms
5,796 KB
testcase_15 AC 4 ms
5,920 KB
testcase_16 AC 4 ms
5,748 KB
testcase_17 AC 4 ms
5,764 KB
testcase_18 AC 3 ms
5,780 KB
testcase_19 AC 4 ms
5,796 KB
testcase_20 AC 5 ms
5,788 KB
testcase_21 AC 3 ms
5,756 KB
testcase_22 AC 4 ms
5,780 KB
testcase_23 AC 3 ms
5,852 KB
testcase_24 AC 4 ms
5,760 KB
testcase_25 AC 4 ms
5,852 KB
testcase_26 AC 4 ms
5,760 KB
testcase_27 AC 4 ms
5,840 KB
testcase_28 AC 7 ms
6,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000009;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}


int max_n;
const int mn = 100000;
struct edge {
	int to, cap; ll cost; int rev;
};
vector<edge> G[mn];
P par[mn];
ll dist[mn];
void add_edge(int from, int to, int cap, ll cost) {
	G[from].push_back({ to,cap,cost,(int)G[to].size() });
	G[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });
	max_n = max({ max_n, from + 1, to + 1 });
}
void add_edge2(int from, int to, int cap, ll cost) {
	G[from].push_back({ to,cap,cost,-1 });
	//G[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });
	max_n = max({ max_n, from + 1, to + 1 });
}
LP minimum_road(int s, int t) {
	fill(par, par + max_n, P{ -1,-1 });
	fill(dist, dist + max_n, INF);
	dist[s] = 0;
	priority_queue<LP, vector<LP>, greater<LP>> q; q.push({ 0,s });
	while (!q.empty()) {
		LP p = q.top(); q.pop();
		int id = p.second;
		if (id == t)continue;
		if (p.first > dist[id])continue;
		rep(j, G[id].size()) {
			if (G[id][j].cap > 0) {
				int to = G[id][j].to;
				ll nd = p.first + G[id][j].cost;
				if (nd < dist[to]) {
					dist[to] = nd;
					par[to] = { id,j };
					q.push({ dist[to],to });
				}
			}
		}
	}
	int cur = t;
	int f = mod;
	while (cur != s) {
		int p = par[cur].first, j = par[cur].second;
		if (p < 0)return { -1,-1 };
		f = min(f, G[p][j].cap);
		cur = p;
	}
	cur = t;
	while (cur != s) {
		int p = par[cur].first, j = par[cur].second;
		if (p < 0)return { -1,-1 };
		G[p][j].cap -= f;
		if (G[p][j].rev >= 0) {
			G[cur][G[p][j].rev].cap += f;
		}
		cur = p;
	}
	return { dist[t],f };
}
ll minimum_cost_flow(int s, int t, int k) {
	ll ret = 0;
	rep(i, k) {
		LP z = minimum_road(s, t);
		if (z.first < 0)return -1;
		if (k - i <= z.second) {
			ret += z.first * (k - i); break;
		}
		i += z.second - 1;
		ret += z.first * z.second;
	}
	return ret;
}

void solve() {
	int n; cin >> n;
	vector<int> a, b;
	int d1, d2;
	int sz; cin >> sz;
	d1 = (n+sz-1) / sz;
	rep(i, sz) {
		int x; cin >> x;
		a.push_back(x);
	}
	cin >> sz;
	d2 = (n+sz-1) / sz;
	rep(i, sz) {
		int x; cin >> x;
		b.push_back(x);
	}
	int s = 200,t = 201;
	rep(i, d1) {
		int le = a.size() * i, ri = a.size() * (i + 1);
		int l = le / b.size();
		int r = (ri - 1) / b.size();
		rep(j, a.size()) {
			for (int k = l * b.size(); k < (r + 1) * b.size(); k++) {
				int cost = 1;
				if (a[j] > b[k % b.size()])cost = 0;
				add_edge(j + le, 100 + k, 1, cost);
			}
		}
	}
	int r = n - (a.size() * (d1 - 1));
	int s2 = 202;
	rep(i, a.size()*(d1-1)) {
		add_edge(s, i, 1, 0);
	}
	add_edge(s, s2, r, 0);
	rep(i, a.size()) {
		add_edge(s2, a.size() * (d1 - 1) + i, 1, 0);
	}
	r = n - (b.size() * (d2 - 1));
	int t2 = 203;
	rep(i, b.size()*(d2-1)) {
		add_edge(100 + i, t, 1, 0);
	}
	add_edge(t2, t, r, 0);
	rep(i, b.size()) {
		add_edge(100 + b.size() * (d2 - 1) + i, t2,1, 0);
	}
	int f = minimum_cost_flow(s, t, n);
	//cout << a.size() * d1 << " " << b.size() * d2 << "\n";
	//cout << "? " << f << "\n";
	cout << n - f << "\n";
}





signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
0