贪心算法 001:Gone Fishing

001:Gone Fishing

总时间限制: 2000ms 内存限制: 65536kB
描述
John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,…,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=192). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >= 0 ), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di (di >= 0). If the number of fish expected to be caught in an interval is less than or equal to di , there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch.
Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.
输入
You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.
输出
For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected.
If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.
样例输入

2 
1 
10 1 
2 5 
2 
4 
4 
10 15 20 17 
0 3 4 3 
1 2 3 
4 
4 
10 15 50 30 
0 3 4 3 
1 2 3 
0 

样例输出

45, 5 
Number of fish expected: 31 

240, 0, 0, 0 
Number of fish expected: 480 

115, 10, 50, 35 
Number of fish expected: 724 

解题思路
难点:
不知道该停在哪个lake,故需要枚举停在每一个lake的情况;
当确定停在某一个lake后,即可算出路上耗费的时间和钓鱼的时间;
利用优先队列,将每个lake按照鱼的数量排序,取队里最大值;
再将该lake的鱼数量属性减掉d[lake序号],入队即可。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue> 
using namespace std;
//Gone fishing

struct lakes{
	int id;
	int fishnum; //5分钟可以钓上的鱼数 
	bool operator<(const lakes &t)const{
				if(fishnum==t.fishnum){
					return id>t.id;            //对应从小到大排序
				}
			return fishnum<t.fishnum;            //对应从大到小排序
	} 
};         //关于优先队列的设定	//https://blog.csdn.net/weixin_43826242/article/details/95221211
int n;
int h;
int t[50]={0};
int d[50];       
int f[50];
lakes L[50];    //里面放最大的n个lakes 
int result;
int Lakes[500];     //注意n是小于25的,故队列数量大于25即可
int Maxresult;
int tempLakes[500];

int main(){
	while(cin>>n){
		if(n==0) return 0;
	cin>>h;
	for(int i=0;i<n;i++){
		cin>>f[i]; 
		L[i].fishnum=f[i];
		L[i].id=i;
	}
	for(int i=0;i<n;i++){
		cin>>d[i];                   //第i个鱼塘,每钓鱼五分钟减少的鱼数 
	}	
	for(int i=1;i<n;i++){       //到第i个鱼塘要经过的距离 
		cin>>t[i];
	}
//创建优先队列
   //默认从大到小排 
	Maxresult=0;
	memset(Lakes,0,sizeof Lakes); 
	
	int sumtime=h*12;
	for (int i=0;i<n;i++){        //取第i个湖 
	
  	priority_queue<lakes> pq;
	for(int k=0;k<=i;k++){         //在循环中创建队列 
		pq.push(L[k]);         //队列中是每个荷塘鱼的最大值 
	}
		memset(tempLakes,0,sizeof tempLakes);         //防止越界拷贝 
		result=0;
		//循环 ,从第几个鱼塘停下
		 sumtime -=t[i];        //减去路费剩余的单位时间
		for(int q=0;q<sumtime;q++){     //循环取出队列中最大值,操作后加入队列
			lakes M=pq.top();            //对头 
			pq.pop();
			result+=M.fishnum;
			tempLakes[M.id]++;
						//记录使用的次数 
			if(M.fishnum-d[M.id]>0){
				M.fishnum-=d[M.id];         
			}
			else{
				M.fishnum=0;	
			}
			pq.push(M);
		}// 循环结束——时间用完
		//比较当前鱼数和最大鱼数
		if(result>Maxresult){
			memcpy(Lakes,tempLakes,sizeof (Lakes));
			Maxresult=result;
		} 
		else if(result ==Maxresult){                   //让序数在前的先被选出 
			for (int F=0;F<sumtime;F++){
				if(Lakes[F]<tempLakes[F]){
					memcpy(Lakes,tempLakes,sizeof (tempLakes));
					break;
				}
				else if (Lakes[F]>tempLakes[F]){
					break;
				}
			}
		}
	 }
	for (int i=0;i<n-1;i++){
		cout<<Lakes[i]*5<<", ";
	}
	cout<<Lakes[n-1]*5<<endl;
	cout<<"Number of fish expected: "<<Maxresult<<endl;
	cout<<endl;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值