南阳acm 第三十题 Gone Fishing翻译+思路

来源:互联网 发布:搜狗 数据分析 面试 编辑:程序博客网 时间:2024/05/16 10:00

Gone Fishing

时间限制:3000 ms  |  内存限制:65535 KB
难度:5
描述
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. 
输入
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. 
输出
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. 
样例输入
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 
样例输出
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 
来源
East Central North A
上传者

张云聪




题意如下:

john去某湖泊钓鱼。他有1到16个小时的整数时间用来钓鱼,湖泊的数量为2到25。john必须从湖泊1开始选择钓鱼,

同时他可以选择从在任何一个湖泊钓鱼。同时呢,他只能从编号小的湖泊往编号大的湖泊前进,也就是说,如果他现在正在3号

湖泊钓鱼,那么他就不能再去1号和2号湖泊钓了。同时,从第i个湖泊到第i+1个湖泊要ti时间的路程。在第i个湖泊边钓鱼,第一

个5分钟可以钓鱼K条,以后再钓鱼5分钟,鱼量减小P。注意这5分钟为最小时间单位

输入的第一行为湖泊的数量n

第二行为钓鱼的可用时间数,以小时记

第三行为n个整数,表示第i个湖泊第一个5分钟的钓鱼数量

第四行为n个整数,表示第i个湖泊再钓鱼5分钟的鱼量减小值p

第五行为n-1个整数,表示从第i个湖泊到第i+1个湖泊所需要的时间数(以5分钟为单位)

如果是4,表示需要20分钟

输入以0表示结束

输出john在每个湖泊的停留时间数以及可能钓到的最大数量的鱼

注意如果存在相同的最优解,那么要选择序号小的湖泊停留时间相对长的那个

主要思想枚举+贪心


#include<iostream>#include<cstdio>#include<cstring>using namespace std;int main(){int n,flag=1;while(cin>>n){if(n==0) break;int i,j,h;int f[30],d[30],t[30],s[30];cin>>h;h=h*12;//将h从60为一个单位转换为以5为一个单位的数  即h=1代表5分钟 for(i=1;i<=n;i++)cin>>f[i];for(i=1;i<=n;i++)cin>>d[i];for(i=2;i<=n;i++)cin>>t[i];t[1]=0;int ff[30],ss[30],sum=0,maxn=-1,max;for(i=1;i<=n;i++)//进行n次枚举 ,每次枚举到至第i个鱼池 {for(j=1;j<=n;j++) ff[j]=f[j];for(j=1;j<=n;j++) ss[j]=0;h=h-t[i];//减去走到鱼池的时间,近似看作瞬移至鱼塘 sum=0;for( j=1; j<=h; j++){max=1;for(int k=1;k<=i;k++)if(ff[max]<ff[k]) max=k;//从fi最大的鱼池开始钓鱼 if(ff[max]<=0)   break;sum+=ff[max];ff[max]-=d[max];ss[max]+=5;//在第max个鱼池逗留5分钟 }if(sum>maxn){int num=0;maxn=sum;for(j=1;j<=n;j++){s[j]=ss[j];num+=ss[j];}s[1]=s[1]+h*5-num;//剩下来的时间全部算在第一个鱼池 }}for(i=1;i<n;i++)cout<<s[i]<<", ";cout<<s[n]<<endl;cout<<"Number of fish expected: "<<maxn<<endl;cout<<endl;}return 0;} 


1 0