POJ 3253 Fence Repair(小顶堆的应用)

Fence Repair

Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 14054

 

Accepted: 4474

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).

Source

USACO 2006 November Gold

 解题报告:给你n块长度已知的木板,已知FJ每次能连接两个木板成为一个新的木板,新的木板长度为两块木板之和。问FJ把n块木板连接起来成最后的一块木板的长度最小是多少?

思路是是哈弗曼算法,但是不太会写,在discuss中看见利用交换小顶堆的元素也能实现;简直不可思议,终于弄懂了;有用到了STL;用时间再研究一下STL,太好使了!

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = 20010;
int Mheap[MAX];
bool cmp(int a, int b) //从大到小排序
{
return a > b;
}
int main()
{
int n, i;
__int64 ans;
while (scanf("%d", &n) != EOF)
{
for (i = 0; i < n; ++i)
{
scanf("%d", &Mheap[i]);
}
ans = 0;
if (n > 1)
{
//make_heap 是按照给定的排序准则,把“最小”的元素排列到首位,而其他元素看上去并非有序:
//相当于建立小顶堆
// 例如:输入 7个数 9 8 3 25 34 0 5 经历操作是 0 8 3 25 34 9 5;
make_heap(Mheap, Mheap + n, cmp);
for (i = n; i > 1; --i)
{
//pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆
//它把0和i个元素交换,然后将[0,i)的数据再做成一个小顶堆堆。
pop_heap(Mheap, Mheap + i, cmp);
ans += Mheap[i - 1];
pop_heap(Mheap, Mheap + i - 1, cmp);
ans += Mheap[i - 2];
Mheap[i - 2] += Mheap[i - 1];
//push_heap()假设由[0,i)是一个有效的堆,
//然后,再把堆中的新元素加进来,做成一个堆。
push_heap(Mheap, Mheap + i - 1, cmp);
}
}
printf("%I64d\n", ans);
}
return 0;
}



原文地址:https://www.cnblogs.com/lidaojian/p/2423719.html