poj 1042 Gone Fishing

来源:互联网 发布:淘宝联盟同时下单 编辑:程序博客网 时间:2024/05/16 09:56
Gone Fishing
Time Limit: 2000MS Memory Limit: 32768KTotal Submissions: 26066 Accepted: 7674

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 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分钟)可以在一个湖中钓走这个湖的鱼,然后这个湖的鱼的数量会减少di个。John一开始在第一个湖,给他一个限定的时间,在限定的时间内每个单位时间(5分钟)他可以选择在一个任意的湖中钓鱼,但是每走到一个湖需消耗一段时间,求这限定的时间内John钓到的最大鱼数。
思路:贪心思想,枚举能走到各个的湖的每种情况,限定的时间依次减去每种情况走到的最远的湖所需的时间,在每种情况下在新的限定的时间内每次选择最大鱼数的湖去钓,直到时间用完或全部鱼钓完。
 
AC代码:
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <queue>#include <cmath>#include <vector>#include <cstdlib>using namespace std;int main(){   int fi[30],di[30],ti[30],h,n;   int num[30];   int ans[30][30];   while(cin>>n,n)   {       cin>>h;       h*=12;       for(int i=0;i<n;i++)            cin>>fi[i];            //湖的初始鱼数       for(int i=0;i<n;i++)       cin>>di[i];            //每钓一次湖中鱼的减少数目       ti[0]=0;       for(int i=1;i<n;i++)       cin>>ti[i];            //走到下一个湖需要消耗的单位时间       memset(ans,0,sizeof(ans));       for(int i=1;i<=n;i++)       {           for(int j=0;j<n;j++)           num[j]=fi[j];           h-=ti[i-1];           int time=h;           int flag=0;           while(time>0&&flag!=i)  //时间用完或者全部鱼钓完           {               int k=0;               for(int j=1;j<i;j++)               if(num[j]>num[k])               k=j;               ans[i][0]+=num[k];               ans[i][k+1]++;               time--;               if(num[k]>0)               {                   num[k]-=di[k];                   if(num[k]<0)                   num[k]=0;               }               flag=0;               for(int j=0;j<i;j++)               if(!num[j]) flag++;           }           ans[i][1]+=time;       }       int m=1;       for(int i=2;i<=n;i++)       if(ans[i][0]>ans[m][0])       m=i;       for(int i=1;i<n;i++)        cout<<ans[m][i]*5<<", ";        cout<<ans[m][n]*5<<endl;        cout<<"Number of fish expected: "<<ans[m][0]<<endl;        cout<<endl;   }   return 0;}

 
原创粉丝点击