POJ 3253 Fence Repair

Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 41285 Accepted: 13462

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into theN 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 theN-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 theN 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个长度的小木块,用来修补农场的围墙,但是他发现自己没有锯子,就找隔壁老王(我瞎翻译的)借锯子,隔壁老王就趁机想黑他一笔,于是就说每锯一段长度为 l 的木板就收你 l 块钱,问如果锯才能花最少的钱得到我们想要的结果


**思路
假设要把长度为9 的木板锯成 三段 长度分别为 2 3 4 的木板,则我们可以逆向思维,把最段的两块组合成一个长木板放回去,然后一步步推到最开始的一块的状态
因为每次都选取目标集合中的最小的两个长度,符合优先队列的结构,这里可以用贪心,也可以直接使用优先队列求解,(关于优先队列问题不懂请自行百度)
这样一来就是一个很简单的切割代价问题
注意切割代价类问题的输出结构一般多用 long long 或者C++的什么64位的整型的我也记不清楚了,不让会wa的
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
	int N;
	while(cin>>N)
	{
		long long l,ans = 0;
		priority_queue<int,vector<int>,greater<int> > que;//小根的优先队列 
		for(int i=0;i<N;i++)
		{
			cin>>l;
			que.push(l);
		}
		while(que.size()>1)//将木块进行合并,直到剩下一块 
		{
			int a1,a2;
			a1 = que.top();
			que.pop();
			a2 = que.top();
			que.pop();
			ans += a1 + a2;
			que.push(a1+a2);
		}
		cout<<ans<<endl;
	}
	return 0;
} 

很简单的题,就是用来熟悉标准库函数的。
奈何我冒泡的算法如何打动你超时的心!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值