c 语言贪心钓鱼思路,POJ-1042 Gone Fishing (贪心法求最佳钓鱼方案

本文介绍了一道关于贪心算法的应用题,John计划一次钓鱼旅行,需要在有限时间内最大化捕鱼量。关键在于合理安排旅行路径和停留时间,利用贪心策略确定每次垂钓的最优湖泊。通过实例解析和代码实现,展示了如何通过贪心法解决实际问题。
摘要由CSDN通过智能技术生成

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

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

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

http___img0.tuicool.com_VFzeE3.jpg

解题思路:

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

http___img1.tuicool.com_6ryYNf.jpg

接下来补充说明一下:

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

我觉得本题思路的一个关键点在于:究竟需要在路上花费多少时间。

通过例子来说明。

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

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

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

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

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

代码:

#include

#include

#include

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

{

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

{

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;

elsebreak;

}//检查是否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<

if(i!=n)cout<

}

cout<

cout<

cin>>n;

if(n!=0)cout<

}

return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值