贪心算法-----poj 3253 Fence Repair(切木板)

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

请读者自己先读一遍英文原题

(题目理解:先计算一共要多长木板?8+5+8=21第一次切割肯定从从长度21的原木板上切一刀,也就是21元的的第一刀的钱花定了,之后就看你会不会精打细算了,注意:每刀的钱数是这刀下去的切开的两段木板长度和!!)

 

编程实现:

法一(简单的哈弗曼编码,运用到直接插入排序)

 

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. using namespace std;  
  7. long long L[20010];  
  8. int main() {  
  9.     int n;  
  10.     while(~scanf("%d", &n)) {  
  11.         int i, j;  
  12.         for(i = 0 ; i < n; i++) {  
  13.             scanf("%I64d", L + i);  
  14.         }  
  15.         sort(L, L + n);  
  16.         long long minn = 0, sum;  
  17.         for(i = 0; i < n - 1; i++) { //n - 1 次后就剩下一根木棍了,此时停止  
  18.             sum = L[i] + L[i + 1];  
  19.             minn += sum;  
  20.             for(j = i + 2; j < n; j++) {  
  21.                 if(sum > L[j]) {  
  22.                     L[j - 1] = L[j];  //向后移动  
  23.                     if(j == n - 1) {  //到了最后一根时放最后  
  24.                         L[j] = sum;  
  25.                     }  
  26.                 }  
  27.                 else {  
  28.                     L[j - 1] = sum; //找到位置  
  29.                     break;  
  30.                 }  
  31.             }  
  32.         }  
  33.         printf("%I64d\n", minn);  
  34.     }  
  35.     return 0;  
  36. }  

 

 

法二(STL优先队列

//木板数目很多时,能提高程序速度

#include  

#include  #include  
#include  // std::greater
using namespace std;
//定义cmp类  类似于sort函数中定义的cmp函数  此题并没有用到  纯属优先权队列尝试  直接使用greater即可  
//class cmp
//{
//public:
//	bool operator()(const __int64 a, const __int64 b)const
//	{
//		return a>b;
//	}
//};
int main()
{
	int n;
	while (scanf("%d", &n) != EOF)//能用scanf就不要用cin  将cin更改为scanf后  时间由47ms降低至17ms   
	{
		priority_queue<__int64, vector<__int64>,greater<__int64> > q;
		//其中的greater若不写的话 则优先权最大的是最大元素   写了greater后  优先权最大的是最小元素  
		//优先权队列有机会的话会再详细探索  
		__int64 len;
		for (int i = 0; i1)//当队列中小于等于一个元素时跳出  
		{
			__int64 a = q.top();  //得到队首元素的值,并使其出队  即最小元素  
			q.pop();
			__int64 b = q.top(); //两次取队首,即得到最小的两个值  即次小元素  
			q.pop();
			mincost += a + b;
			q.push(a + b); //入队   将最小和次小元素的和入队列  
		}
		printf("%I64d\n", mincost);
		while (!q.empty())  //清空队列  
			q.pop();
	}
}

 

参考:https://blog.csdn.net/Strokess/article/details/50995416

 

 

转载必须注明出处:https://blog.csdn.net/qq_34793133/article/details/80663210

你可能感兴趣的:(算法篇,贪心)