【题解】CF1110B:Tape

版权声明:Fashion Education https://blog.csdn.net/ModestCoder_/article/details/86775795

原题传送门
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;
}

猜你喜欢

转载自blog.csdn.net/ModestCoder_/article/details/86775795