Gone Fishing POJ - 1042

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 

约翰正在钓鱼。他有h个小时可用(1 <= h <= 16),并且在该区域有n个湖泊(2 <= n <= 25),所有这些湖泊都沿单一单向道路可到达。约翰从1号湖开始,但他可以在任何他想要的湖上完成。他只能从一个湖到另一个湖,但他不必在任何湖边停下来,除非他希望。对于每个i = 1,...,n-1,从湖i到湖i + 1所需的5分钟间隔的数量被表示为ti(0 <ti≤192)。例如,t3 = 4意味着从3号湖到4号湖需要20分钟的时间。为了帮助计划他的钓鱼之旅,John收集了关于湖泊的一些信息。对于每个湖我都知道在最初的5分钟内捕获的鱼的数量表示为fi(fi> = 0)。每5分钟的捕鱼次数减少预计在接下来的5分钟间隔内以不变率di(di> = 0)捕获的鱼的数量。如果预计在一段时间内捕获的鱼的数量小于或等于di,则在下一个时间段内将不会有更多的鱼留在湖中。为了简化规划,约翰假定没有其他人会在湖边钓鱼,以影响他期望捕获的鱼的数量。
写一个程序,以帮助约翰计划他的钓鱼之旅,以最大限度地增加预计捕获的鱼的数量。在每个湖泊花费的分钟数必须是5的倍数。

这题的贪心策咯不看题解我表示想不出 (我太菜了)time[i]表示到第i个湖所需的时间。通过枚举每一个时间,找出最大值。a1[i]是一个反应a[i]的数组,因为在寻找最大值的时候a[i]的值会变temp[50]也是一个过度数组,存储在每一个湖所花了多少时间 以上应该就是主要思路了
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cctype>
 7 using namespace std;
 8 int a[50],b[50],c[50],time[50],temp[50],a1[50];
 9 int main() {
10     int n,h;
11     while(scanf("%d",&n)!=EOF) {
12         if (n==0) break;
13         scanf("%d",&h);
14         h=h*60;
15         for (int i=0 ; i<n ; i++)
16             scanf("%d",&a[i]);
17         for (int i=0 ; i<n ; i++)
18             scanf("%d",&b[i]);
19         time[0]=0;
20         int t;
21         for (int i=1 ; i<n ; i++) {
22             scanf("%d",&t);
23             time[i]=time[i-1]+t*5;
24         }
25         memset(c,0,sizeof(c));
26         int maxsum=-1;
27         for (int i=0 ; i<n ; i++) {
28             int sum=0,time1=h-time[i];
29             for (int j=0 ; j<n ; j++) {
30                 a1[j]=a[j];
31             }
32             memset(temp,0,sizeof(temp));
33             while(time1>0) {
34                 int maxn=0,x=0;
35                 for (int j=0 ; j<=i ; j++) {
36                     if (a1[j]>maxn) {
37                         maxn=a1[j];
38                         x=j;
39                     }
40                 }
41                 if (maxn==0) break;
42                 time1-=5;
43                 temp[x]+=5;
44                 a1[x]-=b[x];
45                 sum+=maxn;
46             }
47             if (time1>0) temp[0]+=time1;
48             if (sum>maxsum) {
49                 maxsum=sum;
50                 for (int j=0 ; j<=i ; j++)
51                     c[j]=temp[j];
52             }
53         }
54         printf("%d",c[0]);
55         for (int i=1 ; i<n ; i++) {
56             printf(", %d",c[i]);
57         }
58         printf("\n");
59         printf("Number of fish expected: %d\n\n",maxsum);
60     }
61     return 0;
62 }


原文地址:https://www.cnblogs.com/qldabiaoge/p/8526128.html

时间: 2024-11-08 19:59:20

Gone Fishing POJ - 1042的相关文章

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

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 1042 Gone Fishing

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

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

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

贪心算法 -- gone fishing

poj 1042 gone fishing 题目要求: 由有n个湖, 按照顺序排列,一个人从第一个湖向最后一个湖行进(方向只能从湖0到湖n-1),途中可以在湖中钓鱼.在每个湖中钓鱼时,开始的5分钟内可以钓到 f[i] 条,之后每5分钟钓到的鱼数目递减 d[i] ,且每两个相邻的湖的距离 t[i] 给出(t[i] 表示 由第 i 个湖向第 i + 1个湖行进在路上花费5分钟的倍数的时间, 即如果t[3] = 4,则表示从第三个湖向第四个湖行进需要花费20分钟). 现给定总时间h 小时,求出在每个湖

acm刷题记录

我感觉毫无目的地刷题没有意义,便记录每周的刷题,以此激励自己! ----------6.6-------- [vijos1055]奶牛浴场                                      最大化               推荐IOI论文<浅谈用极大化思想解决最大子矩形问题> codeforces 679B - Bear and Tower of Cubes      xjb搞 codeforces  680A - Bear and Five Cards       

HLJU 1046: 钓鱼(数据增强版) (贪心+优化)

1046: 钓鱼(数据增强版) Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 11  Solved: 3 [Submit][Status][Web Board] Description 在一条水平路边,有n个钓鱼湖.从右到左编号为1.2.3--.n. 佳佳有H个小时的空余时间.他希望用这些时间尽可能多的钓鱼.他从湖1出发,向右走,有选择的在一些湖边停留一定时间钓鱼.最后在某一湖边结束钓鱼. 佳佳測出从第i个湖到第i+1个湖须要走Ti分钟的路,还測