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 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
随机推荐
- P3413 SAC#1 - 萌数
题目 洛谷 数位动规用爆搜真好玩 做法 含有回文串实际我们仅需判断是否有\(2/3\)回文串 \(Dfs(now,num,pre,ly,lead,prel,top)\): 在第\(now\)位 \(n ...
- 23种设计模式UML表示形式
一.概况: 类关系表示: 说明: 二.创建型 1.Factory Method 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Met ...
- kubernetes 核心对象
Pods Pod是Kubernetes的基本操作单元,也是应用运行的载体.整个Kubernetes系统都是围绕着Pod展开的,比如如何部署运行Pod.如何保证Pod的数量.如何访问Pod等.另外,Po ...
- HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Gitblit搭建及Git协作开发流程
1. 概述 目前主流的是git作为自己代码管理,但是采用github需要付费才能够使用,如果不付费,代码需要公开.创业团队及小型开发团队都有必要搭建自己的代码服务器,自己摸索需要一定的时间,会赶不及项 ...
- R语言笔记006——分组获取描述性统计量
方法一:使用aggregate()分组获取描述性统计量 aggregate(mtcars[vars],by=list(am=mtcars$am),mean) aggregate(mtcars[vars ...
- spring boot 基础学习
构建微服务:Spring boot 入门篇 http://www.cnblogs.com/ityouknow/p/5662753.html SpringBoot入门系列:第一篇 Hello World ...
- python正则表达式同时匹配多个关键字(多关键字匹配)
网上翻了很多文章...居然没有一个有用的..倒是找到一篇java的,但java的正则表达式和python的还有点不同. 那篇java的文章是用"[keywd1]|[keywod2]|[key ...
- poj2289二分图多重匹配
题意:给你一张二分图,求右边点到汇点的最小容量(保证流量为n)是多少 题解:二分答案,每次重新建边跑最大流,看是不是为n就好了 #include<map> #include<set& ...
- Win10 14316 bash
更新 WSL ~~~ 现在添加删除组件中勾选 Windows Subsystem for Linux 然后运行bash,会提示一个链接,浏览器打开后会出现App Store安装Ubuntu 再次运行b ...