POJ-1042 Gone Fishing (贪心法求最佳钓鱼方案

Gone Fishing

Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 28075   Accepted: 8371

Description

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.

Input

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.

Output

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.

Sample Input

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 

Sample Output

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 

本题在《算法艺术与信息学竞赛》一书中被作为贪心法的例题,题目大意如下:

解题思路:

还是先放书上的讲解,再说说我的理解:

接下来补充说明一下:

按贪心法的原则,要每次都选择能够得到最大收益的湖泊来垂钓,即钓一次鱼之后,就要再次寻找最优的湖前去垂钓,这样看起来的话,如何计算路上花费的时间就是一个很纠结的问题。所以,我觉得本题思路的一个关键点在于:究竟需要在路上花费多少时间。

通过例子来说明。

假设现在有3个湖,编号为1、2、3,现提出两种钓鱼方案:

(1)1->3->2->3->1

(2)1->1->2->3->3

这两种方案,单看路上花费的时间,显然是方案2更优。而这两种方案所得的鱼量呢?仔细想想,事实上是没有区别的。也就是说,只要确定了你最远要走到哪个湖,并按照贪心法的原则作出了第一种方案,那么完全可以将路线转换为方案二,鱼量不会有任何损失。那么你在路上所花费的总时间就已经确定了,也就是一条直线走到最后所需的时间。

Ps:对本题,若所有湖都已经空了,并且时间还有剩余,默认要把剩余时间全部计入第一个湖的时间。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int ans[30][30],f[30],d[30],t[30];
int main()
{
	int h,n;
	cin>>n;
	while(true)
	{
		if(n==0)	break;

		cin>>h;
		h*=12;
		memset(ans,0,sizeof(ans));
		memset(f,0,sizeof(f));
		memset(t,0,sizeof(t));
		memset(d,0,sizeof(d));

		for(int i=1;i<=n;++i)
		{
			scanf("%d",&f[i]);
		}
		for(int i=1;i<=n;++i)
		{
			scanf("%d",&d[i]);
		}
		for(int i=1;i<n;++i)
		{
			scanf("%d",&t[i]);
		}

		int ht,ft[30];
		for(int ed=1;ed<=n;++ed)//枚举最远会走到哪个湖
		{
			memset(ft,0,sizeof(ft));
			for(int i=1;i<=ed;++i)
			{
				ft[i]=f[i];
			}//ft作为f的临时记录
			ht=h;//记录剩余时间
			for(int i=1;i<ed;++i)
			{
				ht-=t[i];//减去路上的时间花费
			}
			//接下来开始模拟钓鱼过程
			int k,emp=1;//emp标记连续的已经空了的湖
			while(ht>0&&emp<=ed)//时间用完或湖空为止
			{
				k=1;
				for(int j=emp;j<=ed;++j)
				{
					if(ft[j]>ft[k])
					{
						k=j;
					}
				}//找出最优的湖

				ans[ed][0]+=ft[k];//此次收获+ft[k]
				++ans[ed][k];//记录在k湖花费了1单位时间
				--ht;//时间消耗1单位
				ft[k]-=d[k];
				ft[k]=ft[k]>0?ft[k]:0;
				for(int j=emp;j<=ed;++j)
				{
					if(ft[j]==0)	++emp;
					else			break;
				}//检查是否ed前的湖都已空
			}
			if(ht>0)	ans[ed][1]+=ht;//若时间有剩余
		}

		int a=1;
		for(int i=2;i<=n;++i)
		{
			if(ans[i][0]>ans[a][0])	a=i;
		}//找出收益最大的方案

		for(int i=1;i<=n;++i)
		{
			cout<<ans[a][i]*5;
			if(i!=n)	cout<<", ";
		}
		cout<<endl;
		cout<<"Number of fish expected: "<<ans[a][0]<<endl;

		cin>>n;
		if(n!=0)	cout<<endl;
	}
	return 0;
}

POJ-1042 Gone Fishing (贪心法求最佳钓鱼方案

时间: 2024-10-13 01:29:02

POJ-1042 Gone Fishing (贪心法求最佳钓鱼方案的相关文章

POJ 1042 Gone Fishing#贪心

(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=30; int n,h,H;//H:记录原本有多少小时的时间:h:贪心的时候,防止H被修改 int res[N],RES[N];//res[]:贪心的时候保存结果:RES[]:用于记录最终结果 int maxn,sum;//maxn:保存最终结果,即捕到的鱼最大值:sum:每轮贪心

POJ 2478 Farey Sequence 筛选法求欧拉函数

题目来源:POJ 2478 Farey Sequence 题意:输入n 求 phi(2)+phi(3)+phi(4)+...+phi(n) 思路:用类似筛法的方式计算phi(1), phi(2), ..., phi(n) 再求前缀和 #include <cstdio> #include <cstring> #include <cmath> //欧拉phi函数 const int maxn = 1000010; typedef long long LL; int eule

poj -- 1042 Gone Fishing(枚举+贪心)

题意: John现有h个小时的空闲时间,他打算去钓鱼.钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(John每在一个湖钓完鱼后,他只能走到下一个湖继续钓),John必须从1号湖开始钓起,但是他可以在任何一个湖结束他此次钓鱼的行程.此题以5分钟作为单位时间,John在每个湖中每5分钟钓的鱼数随时间的增长而线性递减.每个湖中头5分钟可以钓到的鱼数用fi表示,每个湖中相邻5分钟钓鱼数的减少量用di表示,John从任意一个湖走到它下一个湖的时间用ti表示.求一种方案,使得John在有限的h小时中

POJ 1113 Wall 卷包裹法求凸包

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31199   Accepted: 10521 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)

Gone Fishing Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 30281   Accepted: 9124 Description 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 reachab

NYOJ 30 &amp;&amp; POJ 1042 Gone Fishing(枚举+贪心)

[题目链接]:Click here~~ [题目大意]: 一个人去钓鱼,在一条单向路上的旁边有n个湖,并且从湖i到湖i+1需要ti的时间,每个湖里面有fi条鱼,每钓一次鱼,鱼会减少di条.在给定时间T内,问如何才能使钓的鱼最多,并记录在各个湖上停留的时间. [解题思路] 此题细节处理好多麻烦,一定要认真看清题意,POJ WA了无数遍,纠结一天.参考了别人的题解,思路如下: 首先须注意的一点是,John只能向前走,返回的话只会增加John在路上的时间,因而减少他钓鱼的时间.因此此题解题步骤如下: 1

贪心/poj 1042 Gone Fishing

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 struct node 5 { 6 int d; 7 int fish; 8 int ans; 9 }; 10 node a[30],b[30]; 11 int t[30]; 12 int n,h,sum; 13 int main() 14 { 15 scanf("%d",&n); 16 while (n!=0) 17 { 18

贪心法求树的最小支配集,最小点覆盖,最大独立集

定义: 最小支配集:对于图G = (V, E) 来说,最小支配集指的是从 V 中取尽量少的点组成一个集合, 使得 V 中剩余的点都与取出来的点有边相连.也就是说,设 V' 是图的一个支配集,则对于图中的任意一个顶点 u ,要么属于集合 V', 要么与 V' 中的顶点相邻. 在 V' 中除去任何元素后 V' 不再是支配集, 则支配集 V' 是极小支配集.称G 的所有支配集中顶点个数最少的支配集为最小支配集,最小支配集中的顶点个数称为支配数. 最小点覆盖:对于图G = (V, E) 来说,最小点覆盖

POJ 1042 Gone Fishing

题意:一个人要在n个湖中钓鱼,湖之间的路径是单向的,只能走1->2->3->...->n这一条线路,告诉你每个湖中一开始能钓到鱼的初始值,和每钓5分钟就减少的数量,以及湖之间的距离,问用h小时最多钓多少鱼.鱼的数量不会增加,而且如果不钓鱼的话鱼的数量不会减少,如果有多个答案,输出在小号的湖上花费时间最多的答案. 解法:贪心.枚举在前i个湖里钓鱼,那么走的路程就是一定的,用总时间减去走过的时间,剩下的时间每5分钟为一个单位,选鱼最多的湖钓,然后更新湖里鱼的数量.据说dp也可以做,大概