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