zoukankan      html  css  js  c++  java
  • 1117 聪明的木匠 (贪心)

    1117 聪明的木匠
    1 秒 131,072 KB 20 分 3 级题

    思路:

    第一次切割Li, 花费体力:M
    第二次切割Lj,花费体力:M-Li
    第三次切割Ls,花费体力:M-Li-Lj

    N次切割后花费体力为:M-Li-Lj-……-Lk
    那么,花费的总体力为NM-(N-1)Li-(N-2)Lj-……-Lk
    显然,要使得总体力最少,只要Li>Lj>……>Lk

    那么思路很明确了

    代码:

    package _51_node.greedy;
    
    import java.util.PriorityQueue;
    import java.util.Scanner;
    
    public class ex_1177 {
        /**
         * 1177 聪明的木匠
         * 按每段大小由大到小切割就可以使体力最少.
         * 证明:   L1+L2+……+LN=M
         * 第一次切割Li, 花费体力:M
         * 第二次切割Lj,花费体力:M-Li
         * 第三次切割Ls,花费体力:M-Li-Lj
         * 如此下去,
         * N次切割后花费体力为:M-Li-Lj-……-Lk
         * 那么,花费的总体力为NM-(N-1)Li-(N-2)Lj-……-Lk
         * 显然,要使得总体力最少,只要Li>Lj>……>Lk
         */
        static PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
    
        public static void main(String[] args) {
    
            Scanner cin = new Scanner(System.in);
    
            int n = cin.nextInt();
            for (int i = 1; i <= n; i++) {
                int x = cin.nextInt();
                priorityQueue.add(x);
            }
            System.out.println(solve());
        }
    
        public static int solve() {
            int ans = 0;
            while (priorityQueue.size() >= 2) {
                int x = priorityQueue.poll();
                int y = priorityQueue.poll();
                ans += x + y;
                priorityQueue.add(x + y);
            }
            return ans;
        }
    
    }
    
    
  • 相关阅读:
    Java中IO流的总结
    Java常用集合体系以及相互区别
    TreeMap集合特点、排序原理
    HashMap集合
    TreeSet集合
    redis 数据类型详解 以及 redis适用场景场合
    You need tcl 8.5 or newer in order to run the Redis test
    PHP 获取二维数组中某个key的集合
    Linux 定时任务
    phpmailer邮件类
  • 原文地址:https://www.cnblogs.com/somliy/p/10043679.html
Copyright © 2011-2022 走看看