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. ARC 与非ARC 之间的转换,以及如何使一个项目中,ARC与非ARC共存

    1,非ARC 转 ARC的操作 XCode 的 Edit -- Refactor -- Convert to Object-C ARC (注意,一般在一个大项目中,很少直接使用此方法,其正确率有待考虑 ...

  2. HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. mysqldump 的常用操作

    以下是 mysqldump 的一些使用参数 备份数据库#mysqldump 数据库名 >数据库备份名 #mysqldump -A -u用户名 -p密码 数据库名>数据库备份名 #mysql ...

  4. 关于图片上传与下载(Java)

    图片的上传 package com.upload; import java.io.IOException;import java.io.PrintWriter; import javax.servle ...

  5. 描述一下你最常用的编程风格---JAVA

    描述一下你最常用的编程风格---JAVA     描述一下你最常用的编程风格---JAVA   (1)类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都 ...

  6. QT QMimeData类

    http://blog.csdn.net/xie376450483/article/details/5863810 QMimeData类提为数据提供一个容器,用来记录关于MIME类型数据的信息 QMi ...

  7. 初识Django---视图

    MTV模型 一.Django的MTV分别代表:         Model(模型):负责业务对象与数据库的对象       Template(模板):负责如何把页面展示给用户      View(视图 ...

  8. 2017-03-01 Oracle10g的安装与配置使用

    今天项目中又用到了Oracle,时隔三年没有碰过Oracle,之前在做某城市公共自行车管理系统时使用的是Orace10g版本,Oracle给我最大的感觉就是安装上以后,电脑就会变得很卡,所以大家当不使 ...

  9. 配置SSH密码登录

    在客户端生成公钥: ssh-keygen –t rsa 生成的公钥默认位置在~/.ssh/目录 把公钥上传到服务器端: scp id_rsa.pub root@ip地址:文件保存路径 cat id_r ...

  10. IaaS中的统一存储:从设计到实现

    转自:https://www.ustack.com/blog/tycc/ “原生的OpenStack并不支持统一存储,云主机服务Nova.镜像服务Glance.云硬盘服务Cinder的后端存储各不相同 ...