AtCoder Regular Contest 076 F - Exhausted (Hall's marriage theorem 或 贪心)

转化后的题意就是:有N个人,1 ~ M号座位,第i个人愿意坐的座位是[1,Li]或 [Ri,M]。设这个二分图最大匹配是X,然后输出N - X,即问最少有几个人意愿得不到满足。


思路:

在二分图匹配中有个Hall's marriage theorem定理(https://en.wikipedia.org/wiki/Hall%27s_marriage_theorem),这个定理给出了二分图完全匹配的充要条件,即任意X部的子集的元素的个数要不大于其相连的Y部的元素的个数。

这个定理也可以求出最大匹配数,设最小匹配不了的点个数设为p,那么最大匹配的个数就是|X| - p, p等于max (|x| − Γ(x)),其中x是X部的任意子集, Γ(x) 是和x相连的Y部的点的个数。


所以在此题中,我们可以知道对于任意的人的子集X,Γ(X) 必然可以表示成[1,lb]U[ub,M],所以我们可以枚举Γ(X),再求最大的|X|是多少,即枚举(lb, ub), Li <= lb && ub <= Ri的人即可以放在X中。

所以这样可以简单的用O(n ^ 2)的算法解决,用线段树可以优化到O(nlogn)


当然这题也可以贪心做,只不过不够优美 code: http://arc076.contest.atcoder.jp/submissions/1376934


官方题解:https://atcoder.jp/img/arc076/editorial.pdf




原版题意:

Time limit : 2sec / Memory limit : 256MB

Score : 1000 points

Problem Statement

There are M chairs arranged in a line. The coordinate of the i-th chair (1iM) 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 Ri. 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

  • 1N,M2×105
  • 0Li<RiM+1(1iN)
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
L1 R1
:
LN RN

Output

Print the minimum required number of additional chairs.


Sample Input 1

Copy
4 4
0 3
2 3
1 3
3 4

Sample Output 1

Copy
0

The four people can sit in chairs at the coordinates 321 and 4, respectively, and no more chair is needed.


Sample Input 2

Copy
7 6
0 7
1 5
3 6
2 7
1 6
2 6
3 7

Sample Output 2

Copy
2

If we place additional chairs at the coordinates 0 and 2.5, the seven people can sit at coordinates 053261 and 2.5, respectively.


Sample Input 3

Copy
3 1
1 2
1 2
1 2

Sample Output 3

Copy
2

Sample Input 4

Copy
6 6
1 6
1 6
1 5
1 5
2 6
2 6

Sample Output 4

Copy
2


你可能感兴趣的:(二分图及匹配算法)