写在前面
这题目我写了一下午(还是太蒻了),题目给的样例很弱,我第一次做法假了都能过。
题目大意
用AI翻译
给你一些排成两行的盒子,上层 个,下层 个。盒子上的标号从 到 且每个标号只出现一次。开盒子的时候按标号从小到大开。问至少移除多少个盒子(上层或下层的均可)可以保证在开盒子的时候每一个下层的盒子均没有被任何一个上层盒子覆盖(已经开过的视为已被移除)。
分析
我的第一个思路就是贪心,对所有盒子维护一个 数组表示是否被移除,对每个下层的盒子用 vector 记录它被那些上层盒子覆盖(不考虑编号比它小的,因为这样的盒子在下面的盒子之前就已经被打开)。然后按标号从小到大检查,如果当前的下层盒子被多个上层盒子覆盖,那么移除上层盒子不如移除下层盒子;如果只被一个上层盒子覆盖,那么移除上层盒子更有可能更优(因为它有可能还会覆盖临近的盒子)。
代码很好写,但可惜思路是错的。交上去会发现 #WA on 3。
然后我调了好久,一系列尝试后(先 #WA on 8,再 #WA on 9,又 #WA on 29)证明这个贪心是错的(在上下盒子交错排列时可以直接验证,比如下面的 hack 数据)。
4
1 3
2 4
2 6
1 7
3
2 2
1 2
5 2图示如下

红色代表编号,黑色代表长度。
期望输出 3 (也就是移除下面的所有盒子)。
换一个思路,考虑 3 是怎么得出的,思考在上述贪心过程中求出的 vector。为了直观,我们把它标到图中。

蓝色线段链接的两端表示上面的盒子会影响下面的盒子。在这个例子中,因为(会产生相互覆盖的盒子中)上面的编号均比下面的大,所以上下间均有连线。
发现盒子的几何形状没有任何作用,直接把线换成点。
(这不就变成二分图了吗。)
我们把原题意转化到二分图上,问题就变成了: 在二分图上取尽量少的点,使得所有的边均被覆盖 。
(解释:所有被蓝色边所连接的点对,只要其中有一个点被移除就能解除它俩的制约关系。或者说,就是存在相互覆盖关系的点对既可以移除上面的点,也可以移除下面的点。)
当然,这也说明贪心思路中求覆盖关系的过程就相当于建图的过程。
然后这道题就变成 二分图最小点覆盖 的模板了。
如果你没听说过这个东西,你可能需要:Konig 定理 。
Konig 定理:二分图中最小点覆盖的点数等于最大匹配数。
于是这又变成了二分图最大匹配的模板,这一步应该差不多都会了。我这里使用的是用网络流建模用 Dinic 算的最大匹配,其他算法没试过,不过应该也行。
如果你还没学过二分图最大匹配:二分图最大匹配模板(P3386)。
如果你还没学过网络流:网络最大流模板(P3376)。
Code
这份代码的前部分是从我的模板库里复制的。
添加了一些注释。
#ifndef OI_INTERNET_FLOW_HPP
#define OI_INTERNET_FLOW_HPP
#include <iostream>
#include <queue>
template<typename Ty>
class inf {
public:
inline constexpr operator Ty() const {
throw std::invalid_argument("from \"inline operator value_type() const\" in inf<Ty> : "
"Can't use this value.");
}
};//这个 inf 用来重载获得极大值,在不提供明确的模板特化时抛出异常
template<>
class inf<int> {
public:
inline constexpr operator int() const noexcept {
return 0x3f3f3f3f;
}
};//inf 对 int 类型的特化
template<>
class inf<long long> {
public:
inline constexpr operator long long() const noexcept {
return 0x3f3f3f3f3f3f3f3fll;
}
};//inf 对 long long 类型的特化
template<typename Ty>
class Internet_Flow {
//模板参数 Ty 就是流量的数据类型
public:
typedef Ty flow_type;
typedef unsigned long long size_type;
constexpr static size_type npos = static_cast<size_type>(-1);
protected:
struct link {
size_type to;
flow_type full, flow;
size_type nev_id;
flow_type cost;
};
std::vector<std::vector<link>> to;
std::vector<int> dep, cur;
size_type S = 0, T = 0;//源点和汇点
bool auto_reset = true;//是否自动重置
size_type n = 0;
inline void _reset() {
for(auto &i: to)
for(auto &j: i)
j.flow = j.full;
}//将边重置为最开始的状态,在(几乎)所有题里都没用
public:
inline void set_S(const size_type _nS) noexcept { S = _nS; }
inline void set_T(const size_type _nT) noexcept { T = _nT; }
inline size_type get_S() const noexcept { return S; }
inline size_type get_T() const noexcept { return T; }
inline void sync_with_flow(const bool _n) { auto_reset = _n; }
inline bool sync_with_flow() const noexcept { return auto_reset; }
inline void reserve(const size_type _n) {
n = _n;
to.assign(_n, {});
dep.assign(_n, -1);
cur.assign(_n, 0);
}//预留空间,或者说设置大小
inline void add_link(const size_type start, const size_type end, const flow_type flow, const flow_type cost = 0) {
to[start].push_back({end, flow, flow, to[end].size(), cost});
to[end].push_back({start, 0, 0, to[start].size() - 1, cost});
}//添加边,cost 变量是为最小费用最大流保留的,这道题用不到
bool bfs() {
std::queue<size_type> q;
fill(cur.begin(), cur.end(), 0);
fill(dep.begin(), dep.end(), -1);
q.push(S);
dep[S] = 0;
while(!q.empty()) {
size_type now = q.front();
q.pop();
for(size_t i = 0; i < to[now].size(); ++i) {
const link &e = to[now][i];
if(dep[e.to] == -1 && e.flow > 0) {
dep[e.to] = dep[now] + 1;
if(e.to == T)
return true;
q.push(e.to);
}
}
}
return false;
}
inline flow_type dfs(const size_type now, const flow_type limit) {
if(now == T) return limit;
flow_type rest = 0;
for(auto &i = cur[now]; i < to[now].size(); i++) {
link &e = to[now][i];
if(dep[e.to] == dep[now] + 1 && e.flow > 0) {
flow_type take = dfs(e.to, std::min(limit - rest, e.flow));
if(take) {
e.flow -= take;
to[e.to][e.nev_id].flow += take;
rest += take;
if(rest == limit)
break;
}
}
}
if(rest != limit || rest == 0) dep[now] = -1;
return rest;
}
inline flow_type dinic(const flow_type _inf = inf<flow_type>()) {
flow_type ans = 0;
while(bfs())
ans += dfs(S, _inf);
if(auto_reset)
_reset();
return ans;
}//_inf 变量就是上面 dfs 里的 limit,起到限制最大流的作用,也是为最小费用最大流保留的,这道题也用不到
};
#endif //OI_INTERNET_FLOW_HPP
using namespace std;
constexpr int mxn = 1e5 + 1;
constexpr int mxm = 1e5 + 1;
int n, m;
//a 代表上层,b 代表下层
pair<long long, long long> a[mxn], b[mxm];//存储上/下层盒子对应的左端点和右端点
int ida[mxn], idb[mxm];//上/下层从左往右数第 i 个盒子对应的编号是 ina[i]/inb[i]
Internet_Flow<long long> t;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i].second >> ida[i], a[i].first = a[i - 1].second, a[i].second += a[i].first;
cin >> m;
for(int i = 1; i <= m; i++)
cin >> b[i].second >> idb[i], b[i].first = b[i - 1].second, b[i].second += b[i].first;
t.reserve(n + m + 5);//设置大小,省略会RE
for(int i = 1, j = 1; i <= n && j <= m;) {
if(ida[i] > idb[j] && a[i].second > b[j].first && a[i].first < b[j].second)//如果两个盒子有重叠的部分
t.add_link(ida[i], idb[j], 1);
if(a[i].second < b[j].second)//那个落后那个先走
i++;
else if(a[i].second == b[j].second)
i++, j++;
else
j++;
}
for(int i = 1; i <= n; i++)
t.add_link(n + m + 1, ida[i], 1);
for(int i = 1; i <= m; i++)
t.add_link(idb[i], n + m + 2, 1);
t.set_S(n + m + 1);
t.set_T(n + m + 2);
cout << t.dinic();
return 0;
}
如果你 #WA on 32(记录):
不开 long long 见__。
