【题解】CF1110B:Tape

本文探讨了一种高效算法,旨在解决给定长度纸张上多个点的覆盖问题,通过使用最少数量的线段来覆盖所有点,同时确保线段总长度最短。文章介绍了算法思路,避免了高复杂度的DP解决方案,提出了计算最优线段长度的方法。

原题传送门
You have a long stick, consisting of mm segments enumerated from 11 to mm. Each segment is 11 centimeter long. Sadly, some segments are broken and need to be repaired.

You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length tt placed at some position ss will cover segments s,s+1,…,s+t−1s,s+1,…,s+t−1.

You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap.

Time is money, so you want to cut at most kk continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces?

Input
The first line contains three integers nn, mm and kk (1≤n≤1051≤n≤105, n≤m≤109n≤m≤109, 1≤k≤n1≤k≤n) — the number of broken segments, the length of the stick and the maximum number of pieces you can use.

The second line contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤m1≤bi≤m) — the positions of the broken segments. These integers are given in increasing order, that is, b1<b2<…<bnb1<b2<…<bn.
Output
Print the minimum total length of the pieces.
input
4 100 2
20 30 75 80
output
17
input
5 100 3
1 2 4 60 87
output
6
Note
In the first example, you can use a piece of length 1111 to cover the broken segments 2020 and 3030, and another piece of length 66 to cover 7575 and 8080, for a total length of 1717.

In the second example, you can use a piece of length 44 to cover broken segments 11, 22 and 44, and two pieces of length 11 to cover broken segments 6060 and 8787.

题目大意:
总长度为m的纸上,有n个点,点的位置为b数组,现用k条线段覆盖所有的点,求线段总长度最小值

开始想到的是O(n^2)的DP,然后发现n<=100000,果断淘汰
仔细一想,有玄机!
若有一条线段覆盖了两个必须要覆盖的点,那么这两点中间的普通点也被覆盖,线段总长度就是被覆盖的普通点+所有必须覆盖的点
所以我们令被覆盖的普通点最少
然后普通点是一段一段来的,就是两个必须覆盖的点的间隔,所以只要算出间隔,排个序就行了

Code:

/*
4 100 2
20 30 75 80
*/
#include <bits/stdc++.h>
#define res register int
#define ll long long
#define maxn 100010
using namespace std;
int n, m, k, c[maxn];

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	while (c < '0' || c > '9'){
		if (c == '-') w = -1; c = getchar();
	}
	while (c >= '0' && c <= '9') s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
	return s * w;
}

int main(){
	n = read(), m = read(), k = read();
	int a = read(), b = 0;
	for (res i = 2; i <= n; ++ i){
		b = read(); c[i - 1] = b - a - 1; a = b;
	}
	sort(c + 1, c + n);
	int ans = 0;
	for (res i = 1; i <= n - k; ++ i) ans += c[i];
	printf("%d\n", ans + n);
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值