题目链接

Problem Statement
There are M chairs arranged in a line. The coordinate of the i-th chair ($$$1≤i≤M$$$) is $$$i$$$.
N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than Li, or not less than $$$R_i$$$. Naturally, only one person can sit in the same chair.
It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions.
Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs. Constraints

Input
Input is given from Standard Input in the following fomat:
$$$N$$$ $$$M$$$
$$$L_1$$$ $$$R_1$$$
:
$$$L_N$$$ $$$R_M$$$
Output

Print the minimum required number of additional chairs.


Input
4 4
0 3
2 3
1 3
3 4
Output
0
Input
7 6
0 7
1 5
3 6
2 7
1 6
2 6
3 7
Output
2
Input
3 1
1 2
1 2
1 2
Output
2
Input
6 6
1 6
1 6
1 5
1 5
2 6
2 6
Output
2

参考了这篇大佬的博客

【题意】

有$$$m$$$个座位,分别位于坐标为$$$1, 2, 3, ..., m$$$的地方;$$$n$$$个客人,第$$$i$$$位客人只坐位于$$$[0, l_i]\cup[r_i, \infty]$$$的座位。每个座位只能坐一个人,问最少需要添加几个座位才能使所有人坐下?

【分析】

首先说明一下霍尔定理的前提,已知点集X=$$$\{x_1, x_2, ..., x_n\}$$$, Y=$$$\{y_1, y_2, ..., y_m\}$$$,和边集E,E中每条边只能连接 X 与 Y 中的一对点;如果X中的k个点各自经过E与Y中k个不同的点相连,则称形成了一个大小为k的配对。

霍尔定理给出了在X中存在n个点的配对的充分必要条件——对任何一个大小为k的X(k≤n)的子集,Y至少有k个点经过E与它们相连。用公式来表达就是,定义$$$\omega$$$(X)表示Y中一共有几个点经过E与集合X相连,那么霍尔定理就是 $$$\forall$$$ X$$$_1$$$ $$$\subset$$$ X, |X$$$_1$$$| ≤ |$$$\omega$$$(X$$$_1$$$)|

在这道题中,可以这样使用霍尔定理:假设添加t个座位后,所有人都能坐下,也就是存在n个人的配对,那么一定满足对任意一个客人的集合X,有

|X| ≤ |$$$\omega$$$(X)+t|

$$$\Leftrightarrow$$$ |X| ≤ |$$$\omega$$$(X)| +t

$$$\Leftrightarrow$$$ |X| - |$$$\omega$$$(X)| ≤ t

那么t的最小值就是 max{ |X| - |$$$\omega$$$(X)| },因为$$$\omega$$$(X)一定是[1, L]$$$\cup$$$[R,m]的形式,可以改写为L + m - R +1

答案整理为max{ |X| + R - L - m -1 },在霍尔定理的帮助下,解题思路转化为,只需要遍历所有的客人的子集,就能算出来最大值了,不过遍历子集的代价太大,可以转而遍历(L, R)的区间。若固定L,只需要求max{ |X| + R },这个操作可以用线段树来完成,对每个L,处理每个R$$$_i$$$都让[L, R$$$_i$$$]内的数全部+1,为了选择最大的|X| + R,可以把所有结点初始化为R。

由于只关心固定L,[X]+R最大值,为了降低复杂度,可以直接在L$$$_1$$$处理完的基础上继续处理L$$$_2$$$的每个R$$$_i$$$,也就是说L$$$_2$$$不再单独考虑,直接和小的L结合到一起。

【代码】

因为使用了线段树,所以代码量略大,如果不看线段树的代码,其实核心部分很简洁。

#include <stdio.h>
#include <vector>
using std::vector; #define N_max 200005 #define left(x) ((x)<<1)
#define right(x) (((x)<<1)|1)
#define mid(l,r) (l+((r-l)>>1))
#define max(a,b) ((a)>(b)?(a):(b))
int lch[N_max << ], rch[N_max << ], lzy[N_max << ], val[N_max << ]; inline void lzydown(int cur)
{
if(lzy[cur])
{
int l = left(cur), r = right(cur);
lzy[l] += lzy[cur];
lzy[r] += lzy[cur];
val[l] += lzy[cur];
val[r] += lzy[cur];
lzy[cur] = ;
}
} inline void updup(int cur)
{
val[cur] = max(val[left(cur)], val[right(cur)]);
} void build(int cur, int cl, int cr)
{
lch[cur] = cl; rch[cur] = cr;
if (cl == cr) {
val[cur] = cr;
return;
}
int m = mid(cl, cr);
build(left(cur), cl, m);
build(right(cur), m + , cr);
updup(cur);
} void add(int cur,int cl,int cr)
{
if(lch[cur]==cl&&rch[cur]==cr)
{
lzy[cur]++;
val[cur]++;
return;
}
lzydown(cur);
int m = mid(lch[cur],rch[cur]);
if (cr <= m)
{
add(left(cur), cl, cr);
}
else if(cl>m)
{
add(right(cur), cl, cr);
}
else
{
add(left(cur), cl, m);
add(right(cur), m + , cr);
}
updup(cur);
} int query(int cur,int cl,int cr)
{
if (lch[cur] == cl&&rch[cur] == cr)
{
return val[cur];
}
int m = mid(lch[cur], rch[cur]);
lzydown(cur);
if (cr <= m)
{
return query(left(cur), cl, cr);
}
else if (cl>m)
{
return query(right(cur), cl, cr);
}
else
{
int ql= query(left(cur), cl, m);
int qr= query(right(cur), m + , cr);
return max(ql, qr);
}
return -;
} vector<int> ipt[N_max];
int ans = ;
int main()
{
int n, m;
scanf("%d %d", &n, &m);
ans = max(,n - m);
int a[];
for(int i=;i<n;++i)
{
scanf("%d %d", a, a + );
ipt[a[]].push_back(a[]);
}
build(, , m + );
for(int l=;l<=m;++l)
{
int sz = ipt[l].size();
for(int t=;t<sz;++t)
{
int r = ipt[l][t];
add(, , r);
}
int temp = query(, l + , m + ) - m - l - ;
ans = max(ans, temp);
}
printf("%d", ans);
return ;
}

arc076 F - Exhausted? (霍尔定理学习)的更多相关文章

  1. 【AtCoder ARC076】F Exhausted? 霍尔定理+线段树

    题意 N个人抢M个椅子,M个椅子排成一排 ,第i个人只能坐[1,Li]∪[Ri,M],问最多能坐多少人 $i$人连边向可以坐的椅子构成二分图,题意即是求二分图最大完美匹配,由霍尔定理,答案为$max( ...

  2. ARC076 F Exhausted? Hall定理 + 线段树扫描线

    ---题面--- 题目大意: 有n个人,m个座位,每个人可以匹配的座位是[1, li] || [ri, m],可能有人不需要匹配座位(默认满足),问最少有多少人不能被满足. 题解: 首先可以看出这是一 ...

  3. [AtCoder ARC076] F Exhausted?

    霍尔定理 + 线段树? 咱学学霍尔定理... 霍尔定理和二分图完美匹配有关,具体而言,就是定义了二分图存在完美匹配的充要条件: 不妨设当前二分图左端集合为 X ,右端集合为 Y ,X 与 Y 之间的边 ...

  4. [arc076F]Exhausted?[霍尔定理+线段树]

    题意 地上 \(1\) 到 \(m\) 个位置摆上椅子,有 \(n\) 个人要就座,每个人都有座位癖好:选择 \(\le L\) 或者 \(\ge R\) 的位置.问至少需要在两边添加多少个椅子能让所 ...

  5. 【题解】 AtCoder ARC 076 F - Exhausted? (霍尔定理+线段树)

    题面 题目大意: 给你\(m\)张椅子,排成一行,告诉你\(n\)个人,每个人可以坐的座位为\([1,l]\bigcup[r,m]\),为了让所有人坐下,问至少还要加多少张椅子. Solution: ...

  6. Codeforces 1009G Allowed Letters FMT,二分图,二分图匹配,霍尔定理

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1009G.html 题目传送门 - CF1009G 题意 给定一个长度为 $n$ 的字符串 $s$ .并给定 ...

  7. [hdu5503]EarthCup[霍尔定理]

    题意 一共 \(n\) 只球队,两两之间会进行一场比赛,赢得一分输不得分,给出每只球队最后的得分,问能否构造每场比赛的输赢情况使得得分成立.多组数据 \(T\le 10,n\le 5\times 10 ...

  8. [CF981F]Round Marriage[二分+霍尔定理]

    题意 洛谷 分析 参考了Icefox 首先二分,然后考虑霍尔定理判断是否有完美匹配.如果是序列的话,因为这里不会出现 \(j<i,L(i)<L(j)\) 或者 \(j<i,R(i)& ...

  9. [CF1009G]Allowed Letters[贪心+霍尔定理]

    题意 给你一个长为 \(n\) 的串,字符集为 \(a,b,c,d,e,f\) .你可以将整个串打乱之后重新放置,但是某些位置上有一些限制:必须放某个字符集的字符.问字典序最小的串,如果无解输出 &q ...

随机推荐

  1. MySQL高级第二章——索引优化分析

    一.SQL性能下降原因 1.等待时间长?执行时间长? 可能原因: 查询语句写的不行 索引失效(单值索引.复合索引) CREATE INDEX index_user_name ON user(name) ...

  2. 20145226夏艺华 《Java程序设计》第1周学习总结

    http://www.cnblogs.com/bestixyh/p/5779286.html 去年暑假写的,确实比较丑陋,保留下来也是为了激励自己作出更多改变.寒假写的每一篇博客都尽最大努力养成了良好 ...

  3. ORB-SLAM(六)MapPoint与Map

    地图点可以通过关键帧来构造,也可以通过普通帧构造,但是最终,必须是和关键帧对应的,通过普通帧构造的地图点只是临时被Tracking用来追踪用的. 构造函数(地图点3D坐标及其参考帧): // 参考帧是 ...

  4. spring使用set方法注入的常见类型写法

    首先配置spring的pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  5. Vs2015 遇到 CL:fatal error c1510 cannot load language clui.dll

    网上说什么点击修复VS,修改VS的,经验证都不好使,直接下载这个库,放在system32/64下面皆可以了

  6. 180608-Git工具之Stash

    git stash 暂存 背景: 实际开发过程中,经常可能遇到的一个问题,当你在dev分支上正开发得happy的时候:突然来了个线上bug,得赶紧从release分支上切一个bugfix分支来解决线上 ...

  7. Appium(Python)驱动手机淘宝App

    请注意操作步骤: 1. 用数据线连接手机, 打开开发者模式, 并赋予相关权限, 并保持不锁屏状态: 2. 启动Appium桌面服务端: 3. 运行程序: 首次运行, Appium会在手机上安装3个Ap ...

  8. Selenium自动化测试基础

    如有任何学习问题,可以添加作者微信:lockingfree 目录 Selenium自动化测试基础 Selenium自动化测试第一天(上) Selenium自动化测试第一天(下) Selenium自动化 ...

  9. ortp代码简析

    ortp初始化 /** *    Initialize the oRTP library. You should call this function first before using *     ...

  10. 微信小程序之注释出现的问题(.json不能注释)

    js的注释一般是双斜杠// 或者是/**/这样的快注释 .json是配置文件,其内容必须符合json格式内部不允许有注释. JSON有两种数据结构: 名称/值对的集合:key : value样式: 值 ...