command
Version:
v0.0.0-...-0b623e9
Opens a new window with list of versions in this module.
Published: Jun 14, 2022
License: MIT
Opens a new window with license information.
Imports: 6
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
题目
给定 $N$ 个闭区间 $[a_i,b_i]$,请你将这些区间分成若干组,使得每组内部的区间两两之间(包括端点)没有交集,并使得组数尽可能小。
输出最小组数。
输入格式
第一行包含整数 $N$,表示区间数。
接下来 $N$ 行,每行包含两个整数 $a_i,b_i$,表示一个区间的两个端点。
输出格式
输出一个整数,表示最小组数。
数据范围
$1 \le N \le 10^5$,
$-10^9 \le a_i \le b_i \le 10^9$
输入样例:
3
-1 1
2 4
3 5
输出样例:
2
题解
前置题目:0908
前置知识:小根堆
本题知识:贪心-区间问题
思路
- 将所有区间按左端点从小到大排序
- 从前往后处理每个区间,判断能否将其放到某个现有的组中 L[i] > Max_r
- 如果不能,则开新组,然后将其放进去
- 如果能,直接放进去并更新所在组的 Max_r
证明
设答案为 ans,思路所求解为 cnt,证明 ans == cnt
- 因为 cnt 满足题意,而 ans 是满足题意的最小值,故 ans <= cnt
- 设当遍历到第 i 个区间时,前 cnt - 1 组与第 i 个区间都有交集。因为存在交集,所以之前 cnt - 1 个组的 Max_r 都大于 L_i,且因为是按照左端点排序的顺序遍历,所以前 cnt - 1 个组的左端点都小于 L_i。这证明至少有 cnt 个区间存在公共点 L_i。故 ans >= cnt
- ans == cnt ∎
碎碎念
go 的 heap 用的我真是无力吐槽...
Documentation
¶
There is no documentation for this package.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.