1. 程式人生 > >POJ3253-Fence Repair

POJ3253-Fence Repair

extra fin line include 相同 數組 ren lin put

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 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為需要的木板數量,之後n行每行一個數為木板的長度。既然初始木板長度足夠,那我們便可將它視為一塊正合適的木板,從這塊木板正好可以鋸下所以需要的長度零浪費,既然想要耗費最少的錢財,我們可以將切割問題轉化為拼接問題,我們在所有需要鋸下的長度中挑選兩個最小的長度,將它們拼接成一塊,可以得知鋸開我們剛剛拼接的這一塊木板需要考費的錢財就等於它的長度,記錄下這個數字,之後從我們需要沒有鋸的木板中以剛剛拼接好的這一塊代替拼接它的兩塊木板,在選兩塊最小的拼接,以此類推,我們最終可以把所有需要的長度拼成一塊木板,就是我們要鋸的初始木板,鋸開它所耗費的錢財為我們在拼接過程中記錄的數字之和。這就完美的轉化為一個哈夫曼樹問題。   我們用優先隊列來記錄輸入的所有需要鋸下木板的長度,每次取出兩個最小的進行加和成為一段新長度,將這段長度數值加入需要耗費的錢財後,入隊這段新長度,重復操作直到只剩一塊木板。 樣例解析:   3  //木板數量   8  //長度1   5  //長度2   8  //長度3   入隊結束後隊中有 8 8 5 , 5出隊 8出隊,拼接為13,耗費13錢財,13入隊。 隊中現有 13 8,8出隊,13出隊,拼接為21,耗費現為之前的13 + 21 = 34錢財,21入隊,隊中只剩一塊木板,輸出錢財為34。
 1 #include <cstdio>
 2 #include <queue>
 3 // bits/stdc++.h編譯錯誤
 4 using namespace std;
 5 priority_queue<long long, vector<long long>, greater<long long> > value;
 6 //優先隊列設定數值小的先出隊
 7 int main()
 8 {
 9     int n;
10     long long ans = 0;  //ans記錄耗費的錢財
11     while(scanf("%d", &n) != EOF){  //輸入木板數量
12         ans = 0;    //沒有切割是耗費錢財為0
13         while(!value.empty()){  //清空隊列
14             value.pop();
15         }
16         for(int i = 0; i < n; i++){ //輸入並入隊所有長度
17             int temp;
18             scanf("%d", &temp);
19             value.push(temp);
20         }
21         while(value.size() > 1){    //如果隊中所剩木板數量大於則1進行拼接
22             int len1 = value.top();
23             value.pop();
24             int len2 = value.top();
25             value.pop();
26             //出隊兩個最小長度進行拼接操作
27             ans += len1 + len2; //記錄消耗的錢財
28             value.push(len2 + len1);    //新長度入隊
29         }
30         printf("%lld\n", ans);  //輸出答案
31     }
32     return 0;
33 }

POJ3253-Fence Repair