poj3253——哈夫曼树思想 + 优先队列解决

来源:互联网 发布:c语言api怎么用 编辑:程序博客网 时间:2024/05/19 10:54

题目链接:

Fence Repair

题目描述:

Fence Repair
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 37099 Accepted: 12013

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

3858

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

题意:

来自挑战程序设计竞赛:

农夫约翰为了修理栅栏,要将一块很长的木板切割成N块,准备切成的木板的长度为L1,L2,L3...Ln,未切割前木板的长度恰

好为切割后木板长度的总和。每次切断木板时,需要的开销为这块木板的长度,然后最后要求的是总开销。

解析:

例如给定的样例,要把长度为21的木板切割成长度为5,8,8的木板,一开始第一次切割的时候,把长度为21的木板切割成长度13和长

度为8的木板,开销为21,然后第二次切割时,把长度为13的木板切割成长度为5和8的木板,开销为13,至此,切割完毕。总开销

为21 + 13 = 34. 当然你也可以有其他的切割方式,就像样例中的提示的一样,但是开销却更大了,所以要取最小开销的切割方法则

是上述所描述的切割方法。

这个问题看起来是很难入手,但是我们分析一个切割块数更多的木板:

比如说 要切割成5块,每块长度分别是2 4 7 8 9

那么则有如下的切割过程使得开销最低(经过不同切割方式的计算得出):

                       30

 13          17

      6    7     8     9

        2    4

从以上图解,我们可以看出,每块木板的长度可以看成无法再分解下去的木板,即为二叉树中的叶节点。而在这个切割的过程中,总开销为: 30 + 13 + 17 + 6 = 66;

而30又是所以需要得到的木板的长度总和,那么这个表达式又可以写成:

2 + 4 + 7 + 8 + 9 + 13 + 17 + 6 = 66;

推导到了这里,那么问题的求解就很明显了,这个切割木板的问题即可以转换为哈夫曼编码中求最优二叉树(即哈夫曼树)的带权

路径和WPL的问题。而对于这个问题,我们可以用优先队列去求解,完整代码实现:

//哈夫曼编码问题的变种#include<cstdio>#include<queue>using namespace std;typedef long long LL;int N;void solve(){    while(scanf("%d",&N)==1&&N){        priority_queue<LL,vector<LL>,greater<LL> > q;        LL value;        for(int i = 0;i < N;++i){            scanf("%I64d",&value);            q.push(value);        }        LL ans = 0;        while(q.size() > 1){            LL a = q.top();            q.pop();            LL b = q.top();            q.pop();            ans += a + b;            q.push(a+b);        }        printf("%I64d\n",ans);    }}int main(){    solve();    return 0;}

总结:注意问题之间的相互转换与联系,多训练这方面的题目,加强自己的问题转换能力,把无从下手的题目变成自己熟悉的问题。


如有错误,还请指正,O(∩_∩)O谢谢

0 0