首页 > 代码库 > POJ - 3253 Fence Repair

POJ - 3253 Fence Repair

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 lengthLi (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).
 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <queue>
 5 using namespace std;
 6 int main(){
 7      long long  n,x,y;
 8      while(scanf("%I64dd",&n)!=EOF){
 9         priority_queue<int ,vector<int>,greater<int> >q;
10         for(int i=0;i<n;i++){
11             scanf("%d",&x);
12             q.push(x);
13                 }
14                 x=0,y=0;
15         while(q.size()>1){
16             y=q.top();
17             q.pop();
18             y+=q.top();
19             q.pop();
20             x+=y;
21             q.push(y);
22         }
23         q.pop();
24         printf("%I64d\n",x);
25     }
26     return 0;
27 }
28 #include <iostream>
29 #include <algorithm>
30 #include <cstdio>
31 using namespace std;
32 const int MAX = 20010;
33 int Mheap[MAX];
34 bool cmp(int a, int b) //从大到小排序
35 {
36     return a > b;
37 }
38 int main()
39 {
40     int n, i;
41     __int64 ans;
42     while (scanf("%d", &n) != EOF)
43     {
44         for (i = 0; i < n; ++i)
45         {
46             scanf("%d", &Mheap[i]);
47         }
48         ans = 0;
49         if (n > 1)
50         {
51             //make_heap 是按照给定的排序准则,把“最小”的元素排列到首位,而其他元素看上去并非有序:
52             //相当于建立小顶堆
53             // 例如:输入 7个数 9 8 3 25 34 0 5 经历操作是 0 8 3 25 34 9 5;
54             make_heap(Mheap, Mheap + n, cmp);
55             for (i = n; i > 1; --i)
56             {
57                 //pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆
58                 //它把0和i个元素交换,然后将[0,i)的数据再做成一个小顶堆堆。
59                 pop_heap(Mheap, Mheap + i, cmp);
60                 ans += Mheap[i - 1];
61                 pop_heap(Mheap, Mheap + i - 1, cmp);
62                 ans += Mheap[i - 2];
63                 Mheap[i - 2] += Mheap[i - 1];
64                 //push_heap()假设由[0,i)是一个有效的堆,
65                 //然后,再把堆中的新元素加进来,做成一个堆。
66                 push_heap(Mheap, Mheap + i - 1, cmp);
67             }
68         }
69         printf("%I64d\n", ans);
70     }
71     return 0;
72 }
73 /*
74 解题报告:给你n块长度已知的木板
75 已知FJ每次能连接两个木板成为一个新的木板
76 新的木板长度为两块木板之和
77 问FJ把n块木板连接起来成最后的一块木板的长度最小是多少?
78 
79 思路是是哈弗曼算法,但是不太会写
80 在discuss中看见利用交换小顶堆的元素也能实现
81 简直不可思议,终于弄懂了;有用到了STL;用时间再研究一下STL,太好使了!
82 */

 

POJ - 3253 Fence Repair