• 【 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());
    		}
    	}
    }
    
  • 相关阅读:
    angular运行报错“Cannot find module 'ng2-translate'.”
    切换分支
    下载angular项目报错[ERROR] ionic-app-scripts has unexpectedly closed (exit code 1).
    通过原生SQL判断数据是否存在
    多图合并一张长图脚本
    科大讯飞--新冠肺炎检测赛道第八分享
    Mysql定时任务
    Mysql导出数据结构 or 数据
    G6Editor 边的参数配置
    百度坐标转腾讯坐标
  • 原文地址:https://www.cnblogs.com/AWCXV/p/10355874.html
Copyright © 2020-2023  润新知