Description
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 1,000)) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N 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 the N-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 the N 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 make N-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).
package OJ;
import java.util.*;
public class P7_temp { //哈夫曼树,最底层不算 Fence Repair
public static void main(String[] args) {
class HuffmanTree{
Node root; //根节点
ArrayList<Node> nodess;
int amount;//总共需要的数量
Node[] nodes;
public HuffmanTree(int[] ints) {
amount = 0;
nodes = new Node[ints.length];
for(int i=0; i <ints.length; i++){
Node n = new Node(ints[i]);
nodes[i] = n;
}
nodess = new ArrayList<Node>();
}
public void BuildHuffmanTree() {
//首先对数组进行排序
MinHeap mh = new MinHeap(nodes);
mh.CreateMinHeap();
while(mh.nodesSize > 0) {
Node n1 = mh.Delete();
nodess.add(n1);
Node n2 = mh.Delete();
nodess.add(n2);
int i = n1.num + n2.num;
Node n = new Node(i);
n.lchild = n1;
n.rchild = n2;
if(mh.nodesSize == 0){
nodess.add(n);
}
else
mh.Insert(n);
}
root = nodess.get(nodess.size()-1);
}
public void traverse(Node n){
if(n != null && n.lchild !=null) { //假如n有左右孩子,才加n的num
if(n.isVisit == false) {
n.isVisit = true;
amount = amount + n.num;
traverse(n.lchild);
traverse(n.rchild);
}
}
}
class Node{
int num;
// Node parent = null;
Node lchild = null;
Node rchild = null;
boolean isVisit;
public Node(int num) {
this.num = num;
isVisit = false;
}
}
class MinHeap{ // 小根堆,构造Huffman树时需要使用
public Node[] nodes;
public int nodesSize;
public MinHeap(Node[] nodes) {
this.nodes = nodes;
nodesSize = nodes.length;
}
public void CreateMinHeap() {//构造最小堆
AllAdjustUp();
AdjustDown();
}
public Node Delete() {
Node back = nodes[0];
nodes[0] = nodes[nodesSize-1];
nodesSize--;
AdjustDown();
return back;
}
public void Insert(Node i) {
nodes[nodesSize] = i;
nodesSize++;
AdjustUp();
AdjustDown();
}
public void AllAdjustUp() {//自下向上调整,所有节点调整一遍,在构造时侯使用
// int size = nodes.length;
for(int i=nodesSize-1; i>-1; i--) {
//先看数组序号
if(i > 0){
if(nodes[i].num%2 == 0){//假如为偶数,说明是右孩子
if(nodes[i].num > nodes[i-1].num)
i--;//i指向小的孩子
if(nodes[(i-1)/2].num > nodes[i].num) {//假如父亲比孩子大,则交换
Node temp = nodes[i];
nodes[i] = nodes[(i-1)/2];
nodes[(i-1)/2] = temp;
}
}
else {//假如是奇数,说明是左孩子
if(nodes[(i-1)/2].num > nodes[i].num) {//假如父亲比孩子大,则交换
Node temp = nodes[i];
nodes[i] = nodes[(i-1)/2];
nodes[(i-1)/2] = temp;
}
}
}
}
}
public void AdjustUp() { //单独自下向上调整,不用每个节点都调整,在插入时候使用
for(int i=nodesSize-1; i>0;) {
if(nodes[i].num%2 == 0){//假如为偶数,说明是右孩子
if(nodes[i].num > nodes[i-1].num)
i--;//i指向小的孩子
if(nodes[(i-1)/2].num > nodes[i].num) {//假如父亲比孩子大,则交换
Node temp = nodes[i];
nodes[i] = nodes[(i-1)/2];
nodes[(i-1)/2] = temp;
}
}
else {//假如是奇数,说明是左孩子
if(nodes[(i-1)/2].num > nodes[i].num) {//假如父亲比孩子大,则交换
Node temp = nodes[i];
nodes[i] = nodes[(i-1)/2];
nodes[(i-1)/2] = temp;
}
}
i = (i-1)/2;
}
}
public void AdjustDown() { //自顶向下调整
int i = 0;
Node tmp = nodes[i];
int j;
// int length = nodes.length;
for(j=2*i+1; j<nodesSize; j=j*2+1){
if(nodes[j].num>nodes[j+1].num && j<nodesSize-1)//比较hight层的左右孩子
j++; //让j指向小的孩子
if(tmp.num<=nodes[j].num)
break;
else{ //若父亲结点大于孩子结点
nodes[i] = nodes[j]; //则该孩子向上移,hight
i=j;
}//向下移
}
nodes[i]=tmp;
}
}
}
Scanner in = new Scanner(System.in);
int times = in.nextInt();
int[] nodes = new int[times];
for(int i=0; i<times; i++){
int n = in.nextInt();
nodes[i] = n;
}
HuffmanTree ht = new HuffmanTree(nodes);
ht.BuildHuffmanTree();
ht.traverse(ht.root);
System.out.println(ht.amount);
}
}