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
-
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.
题意;有n个湖,每个湖都有fi条鱼,每钓一次就会减少di条鱼,
问一个人在给定的时间内在按照从 1~n 湖中钓的鱼最多是多少,
并输出这个人在些湖中停留的时间
题解: 贪心 + 枚举
注: 枚举所有情况(即1~n的湖)每次找湖中鱼最大的
#include<stdio.h>
#include<string.h>
int f1[30],f2[30];
int t[30],t1[30],t2[30];
int res[30];
int main()
{
int n,m,i,j,ret,q = 0;
int time, time1 , time2;
while(scanf("%d",&n),n)
{
scanf("%d",&m);
time = m * 60;
memset(f2,0,sizeof(f2));
for(i = 0 ; i < n; i ++)
{
scanf("%d",&f1[i]);
f2[i] = f1[i] ;
}
for(i = 0 ; i < n; i ++) scanf("%d",&res[i]);
for(i = 1; i < n;i ++) scanf("%d",&t[i]);
if(q) printf("\n");
q = 1;
int Max = -1;//这个max要小于0
memset(t2,0,sizeof(t2));
for(i = 0 ; i < n; i ++)
{
memset(t1,0,sizeof(t1));
for(j = 0; j < n ; j ++) f1[j] = f2[j];//还原
time1 = time;
for(j = 0 ; j <= i;j ++) time1 -= t[j] * 5;//去湖之间所用的时间
time2 = time1;
int sum = 0;
while(time2 > 0)
{
int max = 0;//注:这个max要等于0
int flag = 0,p=0;
for(j = 0 ; j <= i; j ++) //每次查找鱼最多的湖
{
if(max < f1[j])
{
max = f1[j];
p = j ;
flag = 1;
}
}
if(flag) t1[p] += 5;
sum += max ;
if(f1[p] > 0) f1[p] -= res[p] ;
time2 -= 5;
}
if(Max < sum )
{
Max = sum ;
ret = time1;
for(j = 0 ; j < n ; j ++)
t2[j] = t1[j] ;
}
}
int ans = 0 ;
for(i = 1; i < n;i ++) ans += t2[i];
t2[0] = ret - ans ;
printf("%d",t2[0]);
for(i = 1; i < n;i ++)
printf(", %d",t2[i]);
printf("\n");
printf("Number of fish expected: %d\n",Max);
}
}
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<string>
#include<string.h>
using namespace std;
const int MAX=30;
int fi[MAX],di[MAX],ti[MAX];
int bestresult[MAX]={0},bestfishnum=-1;
int fishing(int end,int fi[],int di[],int ti[],int h)
{
int tmpfi[MAX],cost=accumulate(ti+1,ti+end,0),result[MAX]={0},fishnum=0;
memcpy(tmpfi,fi,sizeof(tmpfi));
while(cost+5<=h*60)
{
int max_pos=max_element(tmpfi+1,tmpfi+end+1)-tmpfi;
//if (tmpfi[max_pos]==0) break;
result[max_pos]++;
fishnum+=tmpfi[max_pos];
tmpfi[max_pos]-=di[max_pos];
if (tmpfi[max_pos]<0) tmpfi[max_pos]=0;
cost+=5;
}
if (fishnum>bestfishnum) {
bestfishnum=fishnum;
memcpy(bestresult,result,sizeof(result));
}
return fishnum;}
void print(int result[],int n,int fishnum)
{ string s;
for(int i=1;i<=n;i++)
{
cout<<s<<result[i]*5;
s=", ";
}
cout<<endl<<"Number of fish expected: "<<fishnum<<endl<<endl;
}
void init()
{
memset(bestresult,0,sizeof(bestresult));
bestfishnum=-1;
}
int main()
{
int n,h;
int m;
while(cin>>n>>h)
{
if (n==0) exit(0);
for(int i=1;i<=n;++i)
{cin>>fi[i];
}
for(int i=1;i<=n;++i)
{ cin>>di[i];
}
for(int i=1;i<n;++i)
{
cin>>m;
ti[i]=5*m;
}
for(int i=1;i<=n;++i)
{
fishing(i,fi,di,ti,h);
}
print(bestresult,n,bestfishnum);
init();
}
}