挑戰上的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;
}

發佈了28 篇原創文章 · 獲贊 1 · 訪問量 6764
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章