Poj 3253 Fence Repair(哈夫曼树)
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
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3
8
5
8
Sample Output
34
Hint
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(哈夫曼树)的更多相关文章
- poj 3253 Fence Repair (哈夫曼树 优先队列)
题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...
- BZOJ 3253 Fence Repair 哈夫曼树 水题
http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...
- POJ 3253 Fence Repair(哈夫曼编码)
题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- POJ 3253 Fence Repair(简单哈弗曼树_水过)
题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
随机推荐
- Python编程-多道技术和进程
一.多道技术 1.多路复用 操作系统主要使用来 记录哪个程序使用什么资源 对资源请求进行分配 为不同的程序和用户调解互相冲突的资源请求. 我们可将上述操作系统的功能总结为: 处理来自多个程序发起的多个 ...
- @MarkFan 口语练习录音 20140518 [超凡蜘蛛侠2-格温的演讲[中文]&驯龙骑士选节口语录音]
一个人看不到未来,就把握不了现在 生命中最值得珍惜的,其实并不是永恒的 正因为它会结束,使其变得弥足珍贵,而且将一去不复返 让我们谨记时间就是运气,所以不要把它浪费在别的生活上 让你的生活过得更有价值 ...
- linux虚拟机ping通主机
右键虚拟机,选择网络适配器,设置为桥接模式.然后关闭主机防火墙,ping就行了(一直ping是没有参数的)
- 图片的另一种展现—将后台图片编码直接展现为图片
1.应用场景 开发过程中,遇到这样的需求:需要将服务器上的图片展现在页面上,但是图片所在服务器不是对外的,图片所在服务器与应用服务器也不在同一台机器上,这时候就需要在开发中先将图 ...
- ajax设置Access-Control-Allow-Origin实现跨域访问
ajax跨域访问 1.jsonp方法,jsonp方法是一种非官方方法,这种方法只支持GET方式, 不如POST方式安全.(即使使用jquery的jsonp方法,type设为POST, 也会自动变为GE ...
- 使用hibernate读取hibernate.cfg.xml文件时碰到这个错误org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/HibernateTest/src/hibernate.cfg.xml]
我的问题在于把hibernate.cfg.xml文件放置在某个包中了,hibernate.cfg.xml文件需要放置在src目录下.
- delphi 数据连接规范
建议大家采用另外一种编码风格,不要在程序中到处都有这种LZ程序生成的代码: begin with qryMain do begin try Close; SQL.Clear; SQL.Add('Del ...
- 算法练习3---水仙花数java版
所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身. java程序代码如下: public class ArithTest { public static void ...
- sql语句中as的用法和作用
最近做项目中,偶然发现在SQL语句中出现了as这个词,一直以来没怎么关注是什么意思,毕竟影响不大,今天有空,就在网上查了一些资料,大概有了一些的了解 我们的Sql语句在很多数据库中都是通用的,比如像M ...
- LeetCode OJ:Binary Tree Paths(二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...