結果

問題 No.200 カードファイト!
ユーザー maine_honzuki
提出日時 2020-12-21 23:13:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 4,449 bytes
コンパイル時間 3,051 ms
コンパイル使用メモリ 214,992 KB
最終ジャッジ日時 2025-01-17 05:45:35
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 WA * 16
権限があれば一括ダウンロードができます

ソースコード

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 << -mcf.flow(s, t).second << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0