挑战上的POJ-3253题解

题目: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).

运用了类似“树”的想法,与Huffman编码相像,由于最佳分割方案是”木板长度*节点深度“之和,所以让短木板更深,同时必有次短木板与它为兄弟节点,详见《挑战程序设计竞赛》48页,也可参照其他博客题解。O(n²)代码如下:

#include<iostream>
using namespace std;
int main()
{
	int n;
	long long sum=0.0,*a;
	cin>>n;
	a=new long long int[n+1];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	while(n>1)//只剩一块木板时结束 
	{
		//求出当前最短和次短的木板 
		int fir=1,sec=2;
		if(a[fir]>a[sec])
			swap(fir,sec);
		for(int i=3;i<=n;i++)//注意下面的两次不是坐标交换而是赋值 
			if(a[i]<a[fir])
				sec=fir,fir=i;
			else if(a[i]<a[sec])
				sec=i;
		//它们之和需要加入sum,并进入父辈进行再排序 
		sum+=a[fir]+a[sec];
		//由于之后要n--,要把第n项和它们之和这两项进入下一回合参与排序 
		if(fir==n)
			swap(fir,sec);
		a[fir]=a[fir]+a[sec];
		a[sec]=a[n];
		n--;
	}
	cout<<sum;
	delete []a;
	return 0;
}


由于是用到了树,我们可以用小根堆来优化时间。将输入数据建成一个最小堆,每次取最小堆上的根节点,连取两次得到的结果相加后重新加入树,只用到了最小堆的插入和删除(各个资料都有介绍,此处并未详解),时间复杂度O(nlogn),代码如下:

#include<iostream>
using namespace std;
long long ans,heap[20001];
int n,heap_size;
void put(long long t)//置入一个数并建立小根堆 
{
	heap[++heap_size]=t;
	int next,now=heap_size;
	while(now>1)
	{
		next=now>>1;
		if(heap[next]<=heap[now])
			break;
		swap(heap[next],heap[now]);
		now=next; 
	}
}
long long get()//取出最小的那个数,即根 
{
	long long re=heap[1];
	heap[1]=heap[heap_size--];
	int now=1,next;
	while(now<<1<=heap_size)
	{
		next=now<<1;
		if(next<heap_size&&heap[next+1]<heap[next])
			next++;
		if(heap[now]<heap[next])
			break;
		swap(heap[now],heap[next]);
		now=next;
	}
	return re;
}
int main()
{
	long long temp;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)//将数据建成一个小根堆,便于取出最小的那个 
		scanf("%lld",&temp),put(temp);
		
	for(;n>1;n--)//取出前两小的数并合成一个再次进入树,从n个木板一直合成到1个木板 
		temp=get()+get(),put(temp),ans+=temp;
	printf("%lld",ans);
	return 0;
}


用STL的话代码会少一些,时间也短一些,代码如下(思路与上面相同,故无注释):
#include<iostream>
#include<queue>
using namespace std;
priority_queue<long long,vector<long long>,greater<long long> >h;
int main()
{
	int n;
	long long temp,x,y,ans=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&x),h.push(x);
	
	for(;n>1;n--)
	{
		x=h.top(),h.pop();
		y=h.top(),h.pop();
		ans+=x+y;
		h.push(x+y);
	}		
	printf("%lld",ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值