Gone Fishing
Posted happy_code
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gone Fishing相关的知识,希望对你有一定的参考价值。
Gone Fishing
John is going on a fising 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
//钓鱼,有n个池塘,每个池塘钓鱼有个可以钓到的期望值,钓5分钟会减少期望值,可以且只能去下一个相邻池塘,并且不能回来,去下一个池塘会耗费一定的时间
问,如何决策可以钓最多鱼。并且要输出在每一个池塘钓的鱼的数量,格式也挺麻烦的
//一道贪心的题目,但是一直误差错误。。。我觉得应该没错了
就是先算出如果最后在每一个池塘结束钓鱼的话,可以钓到的最大的鱼的数量。用一个数组保存,然后,再遍历找到最大值输出就行了,
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 int f_i[30];//初始期望值 6 int temp_i[30];//等于上面的 7 int d_i[30];//期望递减值 8 int t_i[30];//跨池塘耗费时间 9 int n,h; 10 11 int spend[30][30];//在每一个池塘待的时间,spend[][0]是存在这个池塘钓鱼的期望值 12 int Diao(int k) 13 { 14 memset(spend[k],0,sizeof(spend[k])); 15 int i,res=0,time=h*60; 16 for (i=1;i<=k;i++) 17 temp_i[i]=f_i[i]; 18 for (i=1;i<k;i++)//路上的耗费都去掉 19 time-=t_i[i]*5; 20 while (time>0) 21 { 22 int max_=-999999,max_p; 23 for (i=1;i<=k;i++)//每次找到最大的 24 { 25 if (temp_i[i]>max_) 26 { 27 max_=temp_i[i]; 28 max_p=i; 29 } 30 } 31 if (max_>0)//还可以钓鱼 32 { 33 res+=max_; 34 temp_i[max_p]-=d_i[max_p]; 35 spend[k][max_p]+=5; 36 time-=5; 37 } 38 else 39 { 40 spend[k][1]+=time; 41 break; 42 } 43 } 44 spend[k][0]=res;//0位置放期望值 45 return 0; 46 } 47 48 int main() 49 { 50 while (scanf("%d",&n)&&n) 51 { 52 scanf("%d",&h); 53 int i; 54 for (i=1;i<=n;i++) 55 scanf("%d",&f_i[i]); 56 for (i=1;i<=n;i++) 57 scanf("%d",&d_i[i]); 58 for (i=1;i<n;i++) 59 scanf("%d",&t_i[i]); 60 for (i=1;i<=n;i++)//在每一个池塘结束可钓最大值 61 Diao(i); 62 int ans=0,pos; 63 for (i=1;i<=n;i++) 64 { 65 if (spend[i][0]>ans) 66 { 67 ans=spend[i][0]; 68 pos=i; 69 } 70 else if (spend[i][0]==ans)//等于,就要比谁在前面的池塘待的久 71 { 72 for (int j=1;j<=n;j++) 73 { 74 if (spend[i][j]>spend[pos][j]) 75 { 76 pos=i; 77 break; 78 } 79 else if (spend[i][j]<spend[pos][j]) 80 break; 81 } 82 } 83 } 84 for (i=1;i<n;i++) 85 printf("%d, ",spend[pos][i]); 86 printf("%d\\n",spend[pos][i]); 87 printf("Number of fish expected: %d\\n\\n",ans); 88 89 } 90 return 0; 91 }
以上是关于Gone Fishing的主要内容,如果未能解决你的问题,请参考以下文章