版本比较
题解:P17199 [KOI 2026 #2] 工厂
正文
增加 155 行删除 460 行
1123345567789910111112131314151516171819202117222319242521262723282925263031283233303435323637383940344142364344454638474840495042515244535446555648575850515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191205960616263125126127128129130131132133134135646513713813966676869707172142143144145146737414814915015115215315415515615715815916016116216316416516616716816917017117217375767778798081176177178179180181182183184185186187188189190828384858619319419519619719819920020187888990919293949596979899100101204205102103104208209210211212213214215216105106107108109110111112113114115218219220221222223224225226227228229116117118119233234235236237238239240241242243244245246247248249120121122123124125251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288126127128129130291292293294295296297298299300301302303304305306307308309310311312313314315131132133134135136137138322323324325326327328329330331332333139140141142143144145146147148149150151152336337338339340341153154155156157158159160343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440161162443444445446447448449450451452163164165166454455456457458459460461462463464465167168169170171172173174175176177178469470471472473474475476477478479480481482483179180181182183184185186187188189190191192193194195196197198199200488489490491492493494495496497498201202203204205206207208209210502503504505506507508509510511512513514515516517518211212213214215520521522523524525526527528216217218219220221222530531223224225226227228229230231232# 题意简述本题可以转化为一个只有 $O(N)$ 个点、$O(N)$ 条边的最小割问题。用 Dinic 求解,总时间复杂度为 $O(N^3)$,并且可以直接从最小割还原雇用名单。
每名员工只在所属日期的白天以及前后两个夜间工作。所有分组都按照熟练度从小到大,将相邻两人配成一组。**用前缀奇偶性表示雇用方案。**
需要选择员工,使每天选中的人数均为偶数,并最大化“生产利润减去基本工资与夜间补贴”。第 $d$ 天夜间工作的员工,恰好来自第 $d$ 天和第 $d+1$ 天白天工作的两批员工。因此,只要保证每一天白天雇用的人数为偶数,所有夜间的人数也一定为偶数。约定第 $0$ 天和第 $T+1$ 天白天没有员工,就能统一处理最前、最后两个夜间。
# 思路将同一天的应聘者按熟练度从小到大排列,设编号依次为 $i_1,i_2,\ldots,i_k$。令 $p_j$ 表示前 $j$ 人中被雇用人数的奇偶性,偶数为 $0$,奇数为 $1$。其中 $p_0=p_k=0$,只有 $p_1,\ldots,p_{k-1}$ 是待决定的变量。第 $i_j$ 人被雇用,当且仅当 $p_{j-1}\ne p_j$。所以,任意一组内部状态都唯一对应一个人数为偶数的雇用方案,反之也成立。所有天合计最多只有 $N$ 个变量。
## 按熟练度扫描接下来,将目标取相反数,改为最小化“基本工资 $+$ 夜间补贴 $-$ 生产利润”。我们会把它写成两类费用:某个状态为 $1$ 时产生的费用,以及两个状态不同时产生的非负费用。
将应聘者按照熟练度从小到大排序。**先处理基本工资和白天利润。**
对于每一天 $d$,维护一个二进制变量 $p_d$,表示当前已经扫描的应聘者中,第 $d$ 天被选人数的奇偶性。对于第 $i_j$ 人,基本工资为 $C_{i_j}|p_j-p_{j-1}|$。生产利润也能用同样的两个状态表示:
初始时所有 $p_d=0$。处理完全部应聘者后,也必须满足所有 $p_d=0$,这正是每天选中偶数人的要求。| 前一状态 $p_{j-1}$ | 当前状态 $p_j$ | 是否雇用 | 对生产利润的贡献 || --- | --- | --- | --- || $0$ | $0$ | 否 | $0$ || $1$ | $1$ | 否 | $0$ || $0$ | $1$ | 是,作为一组中熟练度较小的人 | $-B_{i_j}$ || $1$ | $0$ | 是,作为一组中熟练度较大的人 | $+B_{i_j}$ |
只要每天选中人数为偶数,每个夜间的人数也一定是偶数:第 $d$ 天夜间工作的员工来自第 $d$ 天和第 $d+1$ 天,两组偶数相加仍然为偶数。因此,生产利润的相反数为 $B_{i_j}(p_j-p_{j-1})$。将同一天所有人的这一项相加,因为首尾状态为 $0$,每个内部状态 $p_j$ 的系数就是 $B_{i_j}-B_{i_{j+1}}$。记所有变量的线性系数为 $h_u$,便得到若干项 $h_up_u$,其中 $h_u$ 允许为负数。
## 一名应聘者产生的代价**夜间补贴可以拆成相邻熟练度之间的费用。**
我们改为最小化“总工资减去生产利润”,最终答案取相反数。考虑某个夜间,把该夜的所有应聘者按熟练度排序。对于两个相邻的熟练度 $a<b$,设熟练度不超过 $a$ 的已雇用人数为 $q$。如果 $q$ 为奇数,就有一组员工的两人分别位于这段空隙两侧,因而这段长度 $b-a$ 要计入夜间补贴;如果 $q$ 为偶数,就没有配对跨过这段空隙。这是因为员工总是按顺序两两配对:前缀人数为奇数,恰好表示还有一人需要与后面的人配对。将所有空隙的贡献相加,就得到了每组熟练度之差的总和。即使某个划分位置对应的应聘者没有被雇用,也只是把一段长度拆开,不影响总费用。
考虑熟练度排序后的某名应聘者,其所属日期为 $d$。设选择前后的奇偶性分别为 $x,y$。第 $d$ 天夜间的员工来自白天的第 $d$、$d+1$ 天。扫描到某个熟练度时,设这两天各自的前缀奇偶状态为 $u,v$,那么该夜的前缀奇偶性就是 $u\oplus v$。因此,在状态不变、长度为 $w$ 的一段熟练度区间上,夜间费用恰好是 $w|u-v|$。
- 不选择该应聘者时,$x=y$。- 选择该应聘者时,$x\ne y$。对每个夜间,用双指针合并两天按熟练度排序的应聘者列表。遇到下一个应聘者前,两个状态保持不变,为它们加入权重等于熟练度差的费用;随后将所属那一天的状态切换到该应聘者之后的前缀状态。每一天扫描开始前、结束后的状态都是固定的 $0$。每名应聘者只出现在两个夜间的合并中,所以全部夜间合计只会产生 $O(N)$ 项费用,无需为每一天枚举所有熟练度。
基本工资产生代价 $C|x-y|$。**将这些费用转化为最小割。**
如果发生 $0\to1$ 的变化,这名员工是当天某一组中熟练度较低的人,对生产利润贡献 $-B$,所以对所求代价贡献 $+B$。目前,总费用已经写成
如果发生 $1\to0$ 的变化,这名员工是熟练度较高的人,对生产利润贡献 $+B$,所以对代价贡献 $-B$。$$E=\sum_u h_up_u+\sum_{(u,v)}w_{u,v}|p_u-p_v|,\qquad w_{u,v}\ge 0.$$
因此,该应聘者带来的基本工资与白天生产代价之和为 $C|x-y|+B(y-x)$。建立源点 $S$ 和汇点 $Z$,约定源点一侧的变量取 $0$,汇点一侧的变量取 $1$。所有固定为 $0$ 的首尾状态直接使用源点表示。
## 将夜间补贴拆到熟练度间隙- 对于 $w|p_u-p_v|$,在 $u,v$ 之间连两条方向相反、容量均为 $w$ 的边。两点在同一侧时没有割边;在不同侧时恰好有一条从源点侧指向汇点侧的边被割,费用为 $w$。- 对于 $h_up_u$,若 $h_u\ge 0$,连边 $S\to u$,容量为 $h_u$,恰好在 $p_u=1$ 时付费。- 若 $h_u<0$,将它改写为 $h_u+(-h_u)(1-p_u)$。把 $h_u$ 加入常数 $\mathrm{base}$,再连边 $u\to Z$,容量为 $-h_u$。这条边恰好在 $p_u=0$ 时被割。
在熟练度排序后的相邻两人之间,设熟练度差为 $\Delta A$。由上述逐项对应可知,每种状态赋值的原费用都等于 $\mathrm{base}$ 加上对应割的容量。因此,最小割给出了所有合法雇用方案中的最小费用;所求最大净收益就是 $-(\mathrm{base}+\mathrm{mincut})$。
考虑第 $d$ 天夜间。该夜工作的员工来自第 $d$ 天和第 $d+1$ 天。扫描完当前前缀后,这个夜间已选员工数的奇偶性为 $p_d\mathbin{\operatorname{xor}}p_{d+1}$。最大流结束后,在残量网络中从源点可达的变量取 $0$,其他变量取 $1$。再扫描每一天的应聘者,当相邻两个前缀状态不同时,输出对应应聘者的编号。由于每天的首尾状态均为 $0$,恢复出的员工人数必然为偶数。
若这个奇偶性为 $1$,说明当前有一组员工的较低熟练度端点已经出现,而较高熟练度端点尚未出现。因此当前熟练度间隙 $\Delta A$ 会被计入该组的夜间补贴。**复杂度分析。**
所以,这个间隙对第 $d$ 天夜间产生的代价为 $\Delta A\,|p_d-p_{d+1}|$。每天排序合计需要 $O(N\log N)$ 时间。变量个数、基本工资边数和线性费用边数均为 $O(N)$;所有相邻天的合并总长度为 $O(N+T)=O(N)$,所以整个网络有 $O(N)$ 个点和边。
为了统一处理边界,定义 $p_0=p_{T+1}=0$。第 $0$ 天夜间对应 $|p_0-p_1|$,第 $T$ 天夜间对应 $|p_T-p_{T+1}|$。Dinic 的一般时间复杂度为 $O(V^2E)$,代入后得到总时间复杂度 **$O(N^3)$**,空间复杂度 **$O(N)$**。本题 $N\le 500$,网络规模很小。代码使用 `long long` 保存容量和答案。
于是全部夜间补贴都变成了相邻二进制变量之间的绝对值代价。**参考代码(GNU C++17)。**
# 最小割建模
## 压缩变量数量
如果为每个熟练度前缀和每一天都建立变量,需要 $O(NT)$ 个点。
事实上,一天的奇偶性只有在处理属于该天的应聘者时才可能变化。因此,为每名应聘者建立一个节点,表示处理完这名应聘者后,其所属日期的奇偶性。
维护 `current[d]`,表示第 $d$ 天当前使用的奇偶性节点。初始时所有 `current[d]` 都指向一个固定为 $0$ 的源点。
处理应聘者 $i$ 时:
1. 令 $x=\text{current}[D_i]$,新建变量 $y$。2. 加入代价 $C_i|x-y|+B_i(y-x)$。3. 将 `current[D_i]` 修改为 $y$。4. 若下一名应聘者的熟练度差为 $\Delta A$,对所有相邻日期加入 $\Delta A\,|p_d-p_{d+1}|$,并处理两侧固定为 $0$ 的边界。
处理结束后,使用无穷大代价强制所有 `current[d]` 等于 $0$。
这样只需要 $N$ 个二进制变量。
## 二元代价与割
规定节点位于源点一侧时变量值为 $0$,位于汇点一侧时变量值为 $1$。
对于代价 $w|x-y|$,在两个节点之间连接容量为 $w$ 的无向边。两个变量不同时,这条边恰好被割断。
对于线性项 $kx$:
- 如果 $k\ge0$,在源点与该节点之间连接容量为 $k$ 的边。- 如果 $k<0$,使用恒等式 $kx=k+(-k)(1-x)$,把 $k$ 加入常数项,并在该节点与汇点之间连接容量为 $-k$ 的边。
应聘者的代价 $C|x-y|+B(y-x)$ 可以拆成一个容量为 $C$ 的无向边、旧变量上的线性项 $-Bx$ 和新变量上的线性项 $+By$。
夜间补贴全部是 $w|x-y|$ 的形式,也可以直接用无向边表示。
图的最小割等于“总工资减去生产利润”的最小值。最终答案是最小割对应代价的相反数。
平行的无向边可以先在矩阵中合并,从而将边数控制在 $O(N^2)$。
# 恢复方案
求出最大流后,从源点沿剩余容量大于 $0$ 的边搜索,得到最小割的源点侧。
对于每名应聘者,比较处理前后的两个奇偶性节点:
- 两个节点位于同一侧,奇偶性没有变化,不雇用。- 两个节点位于不同侧,奇偶性发生翻转,雇用该应聘者。
最终奇偶性被强制为 $0$,因此恢复出的每一天都恰好雇用了偶数人。
# 正确性说明
任意合法雇用方案都能唯一确定各天奇偶性随熟练度扫描发生的变化。选择某名应聘者恰好对应所属日期的奇偶性翻转,白天收益与基本工资产生的代价正是 $C|x-y|+B(y-x)$。
对于每个夜间,一组员工支付的熟练度差等于其两个端点之间所有相邻熟练度差之和。某个熟练度间隙被计入补贴,当且仅当扫描到该间隙时,这个夜间已经出现了奇数名被选员工。因此夜间补贴恰好等于所有 $\Delta A\,|p_d-p_{d+1}|$ 之和。
所以,图割代价与该方案的“总工资减去生产利润”完全相同。反过来,任意满足最终奇偶性为 $0$ 的割也能按变量是否翻转恢复出一个合法方案。
因此最小割对应的方案使“总工资减去生产利润”最小,也就使题目要求的“生产利润减去总工资”最大。
# 复杂度分析
排序时间复杂度为 $O(N\log N)$,生成夜间代价需要 $O(NT)$ 时间。
最小割图有 $O(N)$ 个顶点和 $O(N^2)$ 条边。使用重标号至前算法求最大流,时间复杂度为 $O(N^3)$。
总时间复杂度为 $O(N^3+NT)$;由于 $T\le N$,可写为 $O(N^3)$。空间复杂度为 $O(N^2)$。
# 参考代码
```cpp#include <bits/stdc++.h>using namespace std;using ll = long long;
using int64 = long long;
struct Applicant { int skill; int contribution; int salary; int day; int id;};
struct RelabelToFront {struct Dinic { struct Edge { int to; int reverseIndex; int64 capacity; int to, rev; ll cap; }; int s, t; vector<vector<Edge>> g; vector<int> dep, cur;
int vertexCount; vector<vector<Edge>> graph; vector<int> height; vector<int> currentEdge; vector<int64> excess; Dinic(int n, int s, int t) : s(s), t(t), g(n), dep(n), cur(n) {}
explicit RelabelToFront(int vertexCount) : vertexCount(vertexCount), graph(vertexCount), height(vertexCount, 0), currentEdge(vertexCount, 0), excess(vertexCount, 0) {}
void addUndirectedEdge( int first, int second, int64 capacity ) { Edge forward{ second, static_cast<int>(graph[second].size()), capacity };
Edge backward{ first, static_cast<int>(graph[first].size()), capacity };
graph[first].push_back(forward); graph[second].push_back(backward); void add(int u, int v, ll cap, ll back = 0) { if (u == v || (cap == 0 && back == 0)) return; int a = (int)g[u].size(), b = (int)g[v].size(); g[u].push_back({v, b, cap}); g[v].push_back({u, a, back}); }
void push(int vertex, Edge &edge) { int64 sent = min(excess[vertex], edge.capacity);
if (sent == 0) { return; }
edge.capacity -= sent;
graph[edge.to][edge.reverseIndex].capacity += sent;
excess[vertex] -= sent; excess[edge.to] += sent; // 两个状态不同时支付 w;源点表示固定状态 0。 void penalty(int u, int v, ll w) { add(u, v, w, w); }
void relabel(int vertex) { int minimumHeight = numeric_limits<int>::max();
for (const Edge &edge : graph[vertex]) { if (edge.capacity > 0) { minimumHeight = min(minimumHeight, height[edge.to]); bool bfs() { fill(dep.begin(), dep.end(), -1); queue<int> q; q.push(s); dep[s] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (const auto &e : g[u]) { if (e.cap > 0 && dep[e.to] == -1) { dep[e.to] = dep[u] + 1; q.push(e.to); } } }
height[vertex] = minimumHeight + 1; return dep[t] != -1; }
void discharge(int vertex) { while (excess[vertex] > 0) { if (currentEdge[vertex] == static_cast<int>( graph[vertex].size() )) { relabel(vertex); currentEdge[vertex] = 0; continue; ll dfs(int u, ll lim) { if (u == t) return lim; for (int &i = cur[u]; i < (int)g[u].size(); ++i) { Edge &e = g[u][i]; if (e.cap == 0 || dep[e.to] != dep[u] + 1) continue; ll f = dfs(e.to, min(lim, e.cap)); if (f) { e.cap -= f; g[e.to][e.rev].cap += f; return f; }
Edge &edge = graph[vertex] [currentEdge[vertex]];
if (edge.capacity > 0 && height[vertex] == height[edge.to] + 1) { push(vertex, edge); } else { ++currentEdge[vertex]; } } return 0; }
int64 maximumFlow(int source, int sink) { height[source] = vertexCount;
for (Edge &edge : graph[source]) { int64 sent = edge.capacity;
if (sent == 0) { continue; }
edge.capacity = 0;
graph[edge.to][edge.reverseIndex] .capacity += sent;
excess[source] -= sent; excess[edge.to] += sent; ll flow() { ll ans = 0; while (bfs()) { fill(cur.begin(), cur.end(), 0); while (ll f = dfs(s, (1LL << 60))) ans += f; }
vector<int> vertices; vertices.reserve(vertexCount - 2);
for (int vertex = 0; vertex < vertexCount; ++vertex) { if (vertex != source && vertex != sink) { vertices.push_back(vertex); } }
int index = 0;
while (index < static_cast<int>(vertices.size())) { int vertex = vertices[index]; int oldHeight = height[vertex];
discharge(vertex);
if (height[vertex] > oldHeight) { vertices.erase( vertices.begin() + index );
vertices.insert( vertices.begin(), vertex );
index = 0; } else { ++index; } }
return excess[sink]; // 最后一次 BFS 的 dep 同时给出残量网络中源点可达的点。 return ans; }};
vector<char> sourceSide(int source) const { vector<char> reachable( vertexCount, false );
queue<int> queue; reachable[source] = true; queue.push(source);
while (!queue.empty()) { int vertex = queue.front(); queue.pop();
for (const Edge &edge : graph[vertex]) { if (edge.capacity > 0 && !reachable[edge.to]) { reachable[edge.to] = true; queue.push(edge.to); } } }
return reachable; }struct Person { int a, b, c, d;};
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
int n, days; cin >> n >> days;
vector<Applicant> applicants(n);
for (int i = 0; i < n; ++i) { cin >> applicants[i].skill >> applicants[i].contribution >> applicants[i].salary >> applicants[i].day;
applicants[i].id = i; int n, T; cin >> n >> T; vector<Person> p(n + 1); vector<vector<int>> day(T + 2); for (int i = 1; i <= n; ++i) { cin >> p[i].a >> p[i].b >> p[i].c >> p[i].d; day[p[i].d].push_back(i); } for (int d = 1; d <= T; ++d) { sort(day[d].begin(), day[d].end(), [&](int x, int y) { return p[x].a < p[y].a; }); }
sort( applicants.begin(), applicants.end(), [](const Applicant &first, const Applicant &second) { return first.skill < second.skill; // id[i] 表示扫描过应聘者 i 后的奇偶状态节点。 // 每天最后一个状态必须为 0,直接使用源点编号 0。 vector<int> id(n + 1, 0); int tot = 0; for (int d = 1; d <= T; ++d) { for (int j = 0; j + 1 < (int)day[d].size(); ++j) { id[day[d][j]] = ++tot; } );
const int source = n; const int sink = n + 1; const int64 INF = (1LL << 50);
vector<int> currentParityNode( days + 1, source );
vector<int> previousNode(n, source); vector<int64> linearCoefficient(n, 0);
// 合并所有平行的 |x-y| 项。 vector<vector<int64>> cutCapacity( n + 1, vector<int64>(n + 1, 0) );
auto addCutTerm = [&](int first, int second, int64 capacity) { if (first == second || capacity == 0) { return; }
if (first > second) { swap(first, second); }
cutCapacity[first][second] += capacity; };
for (int position = 0; position < n; ++position) { const Applicant &applicant = applicants[position];
int node = applicant.id; int oldNode = currentParityNode[applicant.day];
previousNode[node] = oldNode;
// C|old-new| + B(new-old)。 addCutTerm( oldNode, node, applicant.salary );
if (oldNode != source) { linearCoefficient[oldNode] -= applicant.contribution; }
linearCoefficient[node] += applicant.contribution;
currentParityNode[applicant.day] = node;
if (position + 1 == n) { continue; }
int64 skillDifference = applicants[position + 1].skill - applicant.skill;
// 第 0 天夜间。 addCutTerm( source, currentParityNode[1], skillDifference );
// 第 1 到第 T-1 天夜间。 for (int day = 1; day < days; ++day) { addCutTerm( currentParityNode[day], currentParityNode[day + 1], skillDifference ); }
// 第 T 天夜间。 addCutTerm( source, currentParityNode[days], skillDifference ); }
// 每一天最终必须选偶数人。 for (int day = 1; day <= days; ++day) { addCutTerm( source, currentParityNode[day], INF ); } int s = 0, t = tot + 1; Dinic din(t + 1, s, t); vector<ll> h(t + 1, 0);
int64 constantTerm = 0;
for (int node = 0; node < n; ++node) { int64 coefficient = linearCoefficient[node];
if (coefficient >= 0) { addCutTerm( source, node, coefficient ); for (int d = 1; d <= T; ++d) { int pre = 0; for (int i : day[d]) { int now = id[i]; din.penalty(pre, now, p[i].c); // 生产利润的相反数为 B_i * (当前状态 - 前一状态)。 h[now] += p[i].b; h[pre] -= p[i].b; pre = now; } }
RelabelToFront flow(n + 2);
for (int first = 0; first <= n; ++first) { for (int second = first + 1; second <= n; ++second) { if (cutCapacity[first][second] > 0) { flow.addUndirectedEdge( first, second, cutCapacity[first][second] ); // 第 d 天夜间由白天属于 d 和 d+1 的员工共同工作。 // 双指针合并两天的熟练度,计算相邻事件之间的奇偶费用。 for (int d = 0; d <= T; ++d) { const auto &a = day[d]; const auto &b = day[d + 1]; int i = 0, j = 0, u = 0, v = 0, last = 0; while (i < (int)a.size() || j < (int)b.size()) { bool first = j == (int)b.size() || (i < (int)a.size() && p[a[i]].a < p[b[j]].a); int x = first ? a[i] : b[j]; din.penalty(u, v, p[x].a - last); last = p[x].a; if (first) { u = id[x]; ++i; } else { v = id[x]; ++j; } } }
for (int node = 0; node < n; ++node) { if (linearCoefficient[node] < 0) { constantTerm += linearCoefficient[node];
// k*x = k + (-k)(1-x)。 flow.addUndirectedEdge( node, sink, -linearCoefficient[node] ); ll base = 0; for (int u = 1; u <= tot; ++u) { if (h[u] >= 0) { din.add(s, u, h[u]); } else { base += h[u]; din.add(u, t, -h[u]); } }
int64 minimumCost = constantTerm + flow.maximumFlow(source, sink);
vector<char> sourceSide = flow.sourceSide(source);
vector<int> hired;
for (const Applicant &applicant : applicants) { int node = applicant.id;
if (sourceSide[node] != sourceSide[previousNode[node]]) { hired.push_back(node + 1); } ll cut = din.flow(); vector<int> state(t + 1, 0), ans; for (int u = 1; u <= tot; ++u) { state[u] = (din.dep[u] == -1); }
cout << -minimumCost << '\n'; cout << hired.size() << '\n';
for (int i = 0; i < static_cast<int>(hired.size()); ++i) { if (i > 0) { cout << ' '; for (int d = 1; d <= T; ++d) { int pre = 0; for (int i : day[d]) { int now = state[id[i]]; if (pre != now) ans.push_back(i); pre = now; }
cout << hired[i]; }
cout << -(base + cut) << '\n'; cout << ans.size() << '\n'; for (int i : ans) cout << i << ' '; cout << '\n'; return 0;}
```