poj3253——哈夫曼樹思想 + 優先隊列解決

題目鏈接:

Fence Repair

題目描述:

Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total 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

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

題意:

來自挑戰程序設計競賽:

農夫約翰爲了修理柵欄,要將一塊很長的木板切割成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謝謝

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章