【 Codeforces Global Round 1 B】Tape

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

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

【题解】

一开始假设我们需要n个胶带(即包含每一个点) 然后因为k<=n 所以可能胶带不够用。 那么就得一个胶带跨过两个点。 怎么选择最好呢? 可以把b[i]-b[i-1]-1处理出来排个序。 (优先取较小的花费) 然后取前n-k个累加和sum。 因为每取一个就少用一段胶带. 然后sum+n就是答案了

【代码】

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());
		}
	}
}
posted @ 2019-02-08 08:37  AWCXV  阅读(123)  评论(0编辑  收藏  举报