贪心算法——Fence Repair(POJ 3253)

本文探讨了如何通过贪心算法和霍夫曼树思想,解决农民John修复围栏问题,以最小化切割成本。提供了两种算法实现:一种是O(n^2)的直接方法,另一种是利用优先队列实现的O(nlogn)解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目连接

http://poj.org/problem?id=3253

Fence Repair(POJ 3253)

题目描述

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

思路

开始拿到题目没有思路,后来看书才看出来是贪心和霍夫曼树的思想。仿照构建霍夫曼树找到节点两个最小的节点,进行合并,删除掉原来的这两个进行合并的节点,将合并后的节点节点加入到原来的节点中,然后依次上述操作,直到最后只剩一个节点为止。方法有两种,一个是新开两个变量存储最小值和次小值,依次递归寻找并且合并,直到计算到只有一个木板为止。这种方法的时间复杂度为O(n^2)虽然时间复杂度高但依旧能过。二、在O(n^2)的基础上考虑能否降到O(nlogn)。C++STL中包含了优先队列的实现,这样简单高效了很多。

代码

//POJ 3253 O(n^2)
#include<iostream>
#include<algorithm>
using namespace std;

//防止溢出 
typedef long long ll;   
const int MAX_N = 20000;
//定义输入
int N,L[MAX_N];

void solve()
{
	ll ans = 0;
	
	while(N > 1)
	{
		// 仿照构建霍夫曼树的思想,找当前节点中的最小的两个 
		//首先找到两个最短的木板的下标放到mii1和mii2中,mii1最短,mii2次短
		int mii1 = 0,mii2 = 1; 
		if(L[mii1] > L[mii2])  swap(mii1,mii2);
		
		for(int i = 2;i < N;i++)
		{
			//非常喜欢下面这种条件分支结构,下限条件放在第一个条件中 
			// L[i]<L[mii1]<L[mii2]
			if(L[mii1] > L[i])
			{
				mii2 = mii1;
				mii1 = i;
			}
			// L[mii1]<L[i]<L[mii2]
			else if(L[mii2] > L[i])
				mii2 = i;
		}
		
		//将两个最小的节点进行合并
		int t = L[mii1] + L[mii2];
		ans += t;
		if(mii1 == N-1) swap(mii1,mii2);	
		//想的非常周到,防止下面将两个节点合并之和赋给L[mii1]时,将L[N-1]原来的值覆盖
		//如果不加这一步,那么mii1 = N-1时,L[mii1] = t,L[mii2] = t 
		L[mii1] = t;
		L[mii2] = L[N-1];
		N--;  
	 }  
	 cout<<ans<<endl;
 }
 
 int main()
 {
 	cin>>N;
 	for(int i =0;i<N;i++)
 		cin>>L[i];
 	solve();
  } 
//POJ 3253
//优先队列做法,O(NlogN) 
#include<iostream>
#include<functional> 
#include<queue>
using namespace std;

typedef long long ll;
const int MAX_N = 20000;
int N,L[MAX_N];

void solve()
{
	ll ans = 0;
	
	//声明一个从小到大取出数值的优先队列 
	priority_queue<int, vector<int>, greater<int> > que;
	//将所有节点入队列
	for(int i=0;i<N;i++)
		que.push(L[i]);
	
	//循环只剩一个木板为止
	while(que.size() > 1)
	{
		//取出最小的和次小的
		int l1,l2;
		l1 = que.top();
		que.pop();
		l2 = que.top();
		que.pop();
		
		//合并
		ans +=l1 + l2;
		que.push(l1+l2); 
	 } 
	 cout<<ans;
}
int main()
{
	cin>>N;
	for(int i=0;i<N;i++)
		cin>>L[i];
	solve();
	return 0;
}

最后附上有关C++中优先队列使用的基本用法链接

https://blog.youkuaiyun.com/annfan123/article/details/52201703

这位朋友讲的还挺清楚

感悟

自己有想法,但对实现依旧还很欠缺,学到了C++优先队列的使用,以及贪心的思想,还需要多加练习,争取早日自己实现!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值