Fence Repair(POJ3253)

題目描述 

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.

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

 思路

最後一次切割的兩塊應該爲長度最短的兩塊,因爲這樣才能保證開銷最小。使用貪心算法,不斷將最短的兩塊相加,直到木塊爲1時停止。

答案一

#include<iostream> 
#include<algorithm>
using namespace std;

int main(){
	int n;
	int a[100];
	
	//讀入數據 
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	
	//res保存當前開銷 
	int res=0;
	//直到計算到木板爲1塊爲主 
	while(n>1){
		//將木板塊從小到大排序 
		sort(a,a+n,less<int>());
		//木板塊數減一 
		n--;
		//將最短的兩塊木板的長度相加 
		a[0] = a[0]+a[1];
//		cout<<a[0]<<endl;
		res+=a[0];
		for(int i=1;i<n;i++){
			a[i] = a[i+1];	
		}
	}
	cout<<res;
}

分析:在上面的代碼中,使用sort排序,其實存在算力的浪費,因爲我們不需要將木塊段全部排序,只需要知道最短的兩段木塊長度即可。所以可做如下優化。

答案二

#include<iostream>
using namespace std;

int main(){
	int n;
	int a[2000];

//	讀入數據 
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}

//	res保存當前合計開銷 
	int res=0;
//	直到計算到木板爲1塊爲主 
	while(n>1){
//		找出木板塊中最短的兩塊
		int m1=0,m2=1;
		if(a[m1]>a[m2])	swap(m1,m2);
		for(int i=2;i<n;i++) {
			if(a[i]<a[m1]){
				m2 = m1;
				m1 = i;
			}else if(a[i]<a[m2]){
				m2 = i;
			}
		}

//		將最短的兩塊木板的長度相加 
		res+=a[m1]+a[m2];

		a[m1] = a[m1]+a[m2];
		a[m2] = a[n-1];

//		木板塊數減一 
		n--;
	}
	cout<<res;
}

答案三

C++的<queue>頭文件中包含有數據結構:優先隊列,該數據結構可將所包含的數值自動排序。關於優先隊列可參閱這篇博客

我們可利用優先隊列來獲取長度最短的兩段木塊。代碼如下

#include<iostream>
#include<queue>
using namespace std;

int main(){
	int n,a;
	priority_queue<int, vector<int>,greater<int> > que;

//	讀入數據 
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a;
		que.push(a);
	}

//	res保存當前合計開銷 
	int res=0;
//	直到計算到木板爲1塊爲主 
	while(que.size()>1){
//		找出木板塊中最短的兩塊
		int m1,m2;
		m1 = que.top();
		que.pop();
		m2 = que.top();
		que.pop();
		
//		將最短的兩塊木板的長度相加 
		res+=m1+m2;
		que.push(m1+m2);
//		木板塊數減一 
	}
	cout<<res;
}

參考:《挑戰程序設計競賽》

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