#题面
#题目描述
You are given closed intervals. The -th interval covers and has a positive integer weight . Consider the undirected graph of vertices, where each vertex corresponds to an interval, and there exists an edge between two vertices if and only if the corresponding pair of intervals has a nonempty intersection. For a given list of intervals, we call this graph the interval graph.
Jaehyun wants the graph to not have a connected component of size greater than . To accomplish this, he can delete zero or more intervals. Jaehyun is lazy, so over all possible ways to delete intervals, he will select the way that minimizes the weight of the intervals he has to delete. Print the weight of the deleted intervals after he has made sure all connected components of the interval graph have size at most .
#输入格式
The first line contains two integers and ().
Each of the next lines contains three integers (, ).
#输出格式
Output the sum of the weights of the deleted intervals after Jaehyun’s deletions.
#输入输出样例
样例输入 #1
5 2
1 4 1
3 6 2
5 8 5
7 10 2
9 12 1
样例输出 #1
3
样例解释 #1
One possible solution is to remove the second and fifth intervals.
#思路
有 个区间 ,每个区间有个权值 。将这 个区间当成 个点,如果两个区间它们之间有交(包括端点),那么就在这两个区间之间连边。现在需要删除一些区间,使得每个连通块大小不超过 。输出删除区间最小的权值和。
可以发现,最后肯定是删除经过某些位置的所有区间。
设 表示当前切到位置 ,然后枚举上⼀段切的位置 ,然后考虑 之间的区间只保留价值最⼤的 个,将其他的区间都删掉,但是复杂度是 级别的,无法通过本题。
考虑优化,可以从后往前枚举 依次加入区间,然后用堆维护⼀下前 ⼤即可,时间复杂度 ,可以通过本题。
#代码
1 |
|