【链接】 我是链接,点我呀:)

【题意】

x轴上有m个连续的点,从1标号到m.
其中有n个点是特殊点。
让你用k段区间将这n个点覆盖。
要求区间的总长度最小。

【题解】

一开始假设我们需要n个胶带(即包含每一个点)
然后因为k

【代码】

import java.io.*;
import java.util.*; public class Main { static int N = (int)1e5;
static InputReader in;
static PrintWriter out;
static int b[],a[],n,m,k; public static void main(String[] args) throws IOException{
in = new InputReader();
out = new PrintWriter(System.out);
b = new int[N+10];
a = new int[N+10]; n = in.nextInt();m = in.nextInt();k = in.nextInt();
for (int i = 1;i <= n;i++) b[i] = in.nextInt();
for (int j = 1;j <= n-1;j++) a[j] = b[j+1]-b[j]-1;
Arrays.sort(a, 1,n);
long ans = n;
for (int i = 1;i <= n-k;i++) {
ans = ans + a[i];
}
out.println(ans); out.close();
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}
05-28 14:17
查看更多