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 needs N (1 ≤ N ≤ 20,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).

犯过以下错误:
误用二分
元素删除后未完成堆的调整操作
最小堆写成了最大堆
long类型的res用了int类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.*;

class Heap {
private final int maxn = 50010;
int[] data;
int r;

Heap() {
data = new int[maxn];
r = 0;
}

public int size() {
return r;
}

void swap(int a, int b) {
int tmp = data[a];
data[a] = data[b];
data[b] = tmp;
}

void up(int p) {
if (!(p > 0))
return;
int q = p >> 1;
if (data[p] < data[q]) {
swap(p, q);
up(q);
}
}

void down(int p) {
int q;
if ((p << 1) >= r)
return;
else if ((p << 1) == r - 1) {
q = p << 1;
} else {
q = (data[p << 1] < data[p << 1 | 1] ? p << 1 : p << 1 | 1);
}
if (data[p] > data[q]) {
swap(p, q);
down(q);
}
}

void push(int n) {
data[r++] = n;
up(r - 1);
}

int pop() {
int res = data[0];
swap(0, r - 1);
r--;
down(0);
return res;
}

int top() {
return data[0];
}

}

public class Main {
private static long solve(int[] a) {
Heap hp = new Heap();
int n = a[0], l1, l2;
long res = 0;
for (int i = 1; i <= n; i++)
hp.push(a[i]);
// hp.print();
while (hp.size() > 1) {
l1 = hp.pop();
l2 = hp.pop();
res += (long) (l1 + l2);
hp.push(l1 + l2);
}
return res;
}

public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(System.out);
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
int[] a = new int[n + 1];
a[0] = n;
for (int i = 1; i <= n; i++) {
in.nextToken();
a[i] = (int) in.nval;
}

if (n > 1)
out.println(solve(a));
else
out.println(a[1]);
out.flush();
}
out.close();
}
}