zoukankan      html  css  js  c++  java
  • 【 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());
    		}
    	}
    }
    
  • 相关阅读:
    矩阵树定理 / 生成树计数
    NC20811 蓝魔法师 (树形DP/树上01背包)
    Xor-MST学习/ 2020牛客暑假多校(五)B.Graph
    HDU-6820 Tree (2020杭电多校(五) 1007)
    Flipping Coins (概率DP)
    宝石装箱 容斥+dp
    Rabbit的工作(1) (dP)
    Codeforces-1350 E.Orac and Game of Life
    HDU-6563 Strength (贪心)
    HDU-6558 The Moon (期望DP)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/10355874.html
Copyright © 2011-2022 走看看