Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤
N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤
Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into theN planks (i.e., whose length is the sum of the lengths
Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of theN-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create theN planks. FJ knows that he can cut the board in various different orders which will
result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks

Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to makeN-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.

The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into
16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).



分析:逆向思维,组合各个木板,构造哈夫曼树。在实现哈夫曼树的过程中用优先队列,由于每次往队列里加元素时都要排序,所以实现了Comparator接口,这样就会自动排序了。由于数据较大,输出结果用了大数。

import java.math.BigInteger;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner; class Comparators implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
}
public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
Comparators comparator=new Comparators();
PriorityQueue<Integer> queue=new PriorityQueue<Integer>(20001,comparator);
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
} for(int i=0;i<n;i++){
queue.add(a[i]);
}
BigInteger total = BigInteger.ZERO;
while(queue.size()!=1){
int m=queue.poll();
int l=queue.poll();
int sum=m+l;
queue.add(sum);
total=total.add(BigInteger.valueOf(sum));
}
System.out.println(total);
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj 3253 Fence Repair(哈夫曼树)的更多相关文章

  1. poj 3253 Fence Repair (哈夫曼树 优先队列)

    题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...

  2. BZOJ 3253 Fence Repair 哈夫曼树 水题

    http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...

  3. POJ 3253 Fence Repair(哈夫曼编码)

    题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...

  4. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

  5. POJ 3253 Fence Repair(简单哈弗曼树_水过)

    题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...

  6. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  7. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  8. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  9. poj 3253 Fence Repair(优先队列+哈夫曼树)

    题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...

随机推荐

  1. js原生插件格式解析

    一个合格的插件必须满足以下要求: 1.拥有独立作用域与用户作用域隔离,插件内的私有变量不可影响用户定义的变量 2.拥有默认参数 3.提供配置方法让用户可改变参数 4.提供监听接口,以监听页面操作 5. ...

  2. 20145230《java程序设计》第五次实验报告

    20145230实验五 Java网络编程及安全 实验内容 掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验步骤 本次实验我负责编写客户端代码的编写,以下是我实验进行的步骤: ...

  3. 关闭Selinux 命令

    在nginx 配置文件中,新增location中的内容,完成后,web上403报错 方法:关闭Selinux即可. Follow below steps: 虚拟机服务器环境补充: # vim/etc/ ...

  4. HTML5 JS 压缩图片,并取得图片的BASE64编码上传

    基本过程 1) 调用 FileReader 的 reader.readAsDataURL(img); 方法, 在其onload事件中, 将用户选择的图片读入 Image对象. 2) 在image对象的 ...

  5. JAVA 集成 Ueditor 百度富文本编辑器

    开发环境:一个简单的SpringMVC框架中,用百度富文本编辑器 ueditor 实现图片和文件的上传 官网地址:http://ueditor.baidu.com/website/ 需要使用到的2个文 ...

  6. POJ 3376 Finding Palindromes (tire树+扩展kmp)

    很不错的一个题(注意string会超时) 题意:给你n串字符串,问你两两匹配形成n*n串字符串中有多少个回文串 题解:我们首先需要想到多串字符串存储需要trie树(关键),然后我们正序插入倒序匹配就可 ...

  7. 循环插入一条数据的sql写法

    DECLARE @i INTSET @i = 1WHILE @i > 0 BEGIN DECLARE @TransportFormMstID BIGINT; DECLARE @TradeOrde ...

  8. 虚拟机CentOS6.5网络配置

    不得不说  6.5比7.0麻烦了许多.. 编辑ifcfg配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0 内容如下 DEVICE=eth0 HWADDR ...

  9. js数字过大js自动变0

    解决方案: <a onclick="checkDate(${item.billday},'${item.bankcard}',${item.limitmoney/100}) 加上引号变 ...

  10. servlet中service() doGet() doPost() 方法

    HttpServlet 里的三个方法:service(HttpServletRequest req, HttpServletResponse resp) ,doGet(HttpServletReque ...