nBOJ7 - Fence Repair

Fence Repair
Accept:79     Submit:306
Time Limit:1000MS     Memory Limit:65536KB

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 ≤ 1,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).

 

之前在POJ做过这道题。Huffman树的思路。维护一个优先队列就好了。

可以这么考虑,从最小的两块木板不断合成,然后再选最小的两块(包括刚才合成的这块),直到形成完整的木板,那么此时所用的价格即为最小价格。

注意数据,需要使用long long。同时需要考虑只有一个数据的情况。

 

#include<iostream>
#include<queue>
#include<vector>
#include<cstdlib>
#include<cstdio>

using namespace std;

class cmp
{
public:
bool operator()(long long a,long long b) const
{
return a > b;//最小优先度的队列
}
};

int main(void)
{
priority_queue<long long,vector<long long>,cmp> q;

long long plk,fee(0),tmp;//plk是木板(plank)的总数,fee是总费用,tmp是用来读入的临时变量
scanf("%lld",&plk);
while(plk--)
{
scanf("%lld",&tmp);
q.push(tmp);
}

long long plk_piece(0);//下面解释这个变量的作用

if(q.size() == 1)//如果优先队列只有一个元素,那么队列唯一的元素就是费用
fee = q.top();
else//否则利用huffman树的思路不断弹出两个元素合并为一个(用到那个plk_piece),然后fee上加上这个plk_piece的费用
while(q.size() != 1)
{
plk_piece += q.top();
q.pop();
plk_piece += q.top();
q.pop();
fee += plk_piece;
q.push(plk_piece);//把合成的木板推入队列
plk_piece = 0;
}
printf("%lld\n",fee);

return 0;
}



posted @ 2012-03-02 08:34  codejustforfun  阅读(168)  评论(0编辑  收藏  举报