POJ 3253 Fence Repair(修篱笆)

POJ 3253 Fence Repair(修篱笆)

Time Limit: 2000MS   Memory Limit: 65536K

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

农夫约翰打算修理下围绕牧场的一小段篱笆。他量了量篱笆发觉需要N(1 ≤ N ≤ 20,000)块木板,每块木板分别需要Li(1 ≤ Li ≤ 50,000)整的单位长度。然后他买了块长到可以切出这N块木板的长木板(换句话说,它的长度是Li的总和)。FJ会忽略锯开木板时变成木屑的“锯痕”;所以你也应该忽略它。

FJ悲伤地发现自己没有锯子用来据木头,因此他带着长木板去农夫Don的农场借去了。

怎料农夫Don是个抠门的资本家,并不想借给FJ,但可以有偿帮他切N-1次木板。切木板的费用与木板长度相等。却一块长21的木板需要花21美分。

农夫Don让农夫约翰自己决定怎么切。请帮农夫约翰确定完成这N块板后的最小费用。FJ知道使用不同切割方式会造成不同的花费,因为中途的木板长度不同。

【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

第1行:一个整数N,表示木板的数量。

第2~N+1行:每行一个整数,表示需要木板的长度。

【Output】

【输出】

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

第一行:约翰进行N-1次切割的最小花费

【Sample Input - 输入样例】

【Sample Output - 输出样例】

3

8

5

8

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

约翰想把长度为21的长木板切成8,5,和8.

初始木板的长度为8+5+8=21。初次切割花费21,然后应被切成13与8。第二次切割花费13,然后应被切成8和5。此时的花费是21+13=34。如果21被改切成16和5,第二次切割花费16后总费用为37(这样比34来得大)。

【题解】

  贪心plus,用哈夫曼树的合并思想。

  一开始想先把最短的板分出去,不过算了算结果好像不对。

  应该把一块长板分成尽可能长的两块短板,之后逆向实现就好了。

  需要这里要注意的是数据会超int

【代码 C++】

 1 #include<cstdio>
 2 #include<queue>
 3 #include<functional>
 4 std::priority_queue<int, std::vector<__int64>, std::greater<int> > data;
 5 int main(){
 6     int n, i;
 7     __int64 temp, opt;
 8     scanf("%d", &n);
 9     for (i = opt = 0; i < n; ++i){
10         scanf("%I64d", &temp);
11         data.push(temp);
12     }
13     if (n == 1) printf("%I64d", data.top());
14     else{
15         for (i = 1; i < n; ++i){
16             temp = data.top(); data.pop();
17             temp += data.top(); data.pop();
18             opt += temp; data.push(temp);
19         }
20         printf("%I64d", opt);
21     }
22     return 0;
23 }
原文地址:https://www.cnblogs.com/Simon-X/p/5322395.html