NYOJ 30Gone Fishing(贪心)

来源:互联网 发布:联通软件研究院年终奖 编辑:程序博客网 时间:2024/05/21 06:37

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 
  此题运用贪心思想求解。。。

1题意

  在一条水平的路边有n个湖可以钓鱼,编号一次为1.2.3...n,John有h个小时的时间用于钓鱼,他希望用这些时间钓到尽量多的鱼,他从第一个湖出发,有选择的在一些湖边停留一定的时间钓鱼,最后在某个湖边停止钓鱼,为了制定好的钓鱼计划,John测出了从第 i 个湖走到第 i+1 个湖需要的时间为 5*ti 分钟,还测出了在第 i 个湖边初始的钓鱼数目 fi 和5分钟后减少的数目 di ,求出钓鱼最多的方案,若存在多个最优方案,求出在第一个湖钓鱼时间最长的,若还有多个,则输出在第二个钓鱼最长的方案。。。

2 思路分析

    由于 Join 可以在任意的一个湖结束钓鱼,因此可以枚举他结束钓鱼的湖,求出相应情形下的最优方案然后再这些最优方案中选择最优方案即可。

    假设他在湖 x 结束钓鱼,则用于钓鱼的时间为 Tx = 60h - 5ti(i=123...n)分钟,在下一个 ti 内考虑 John 钓鱼,可以认为从一个湖 u 到 另一个湖 v 所用的时间为 0 。即在任意一个时刻有可以从编号为 1 . 2 . 3 ....x 的湖中任选一个钓5分钟的鱼,(意思即不考虑跑路的时间,先不断的在 x 个湖中选择当前钓鱼量最多的湖钓鱼 ) , 

   对于每个湖来说,任何时候,钓鱼量只和在这个湖钓鱼的次数有关,和总次数没关系,所以可用贪心思想求解。(意思即最后在考虑时间)

3 代码

#include <iostream>#include<stdio.h>#include<stdlib.h>using namespace std;const int N=25;int n,h;int f[N],d[N],t[N]; //f第一个5分钟钓鱼量 d每个5分钟钓鱼减少量 t从i到i+1需要5分钟个数int ans;int each[N]; //采用最优方案是,在i胡的钓鱼时间int tans,teach[N]; //最优钓鱼量和各个湖钓鱼的时间int th,tf[N]; //有效钓鱼时间和每个5分钟钓鱼量int main(){    int i,j;    while(cin>>n && n>0)    {        cin>>h;        for(i=0;i<n;i++)        cin>>f[i];        for(i=0;i<n;i++)        cin>>d[i];        for(i=0;i<n-1;i++)        cin>>t[i];        h*=12;        ans=-1;        for(i=0;i<n;i++)        {            //初始化每次贪心的相关量            th=h;            for(j=0;j<n;j++)            {                tf[j]=f[j];                teach[j]=0;            }            tans=0;            //对每一个5分钟贪心选择钓鱼量最大的湖钓鱼            while(th>0)            {                int ind,max;                ind=0;max=tf[0];                for(j=1;j<=i;j++)                {                    if(tf[j]>max)                    {                        max=tf[j];ind=j;                    }                }                if(max==0) //最大的钓鱼量为0时将剩余钓鱼时间加到第一个湖上的钓鱼时间                {                    teach[0]+=th*5;                    break;                }                else                {                    teach[ind]+=5;                    tans+=tf[ind];                    if(tf[ind]>=d[ind])                      {                          tf[ind]-=d[ind];}                    else tf[ind]=0;                }                th--;            }            //走过的湖个数为i+2时候的钓鱼时间            if(i!=n-1) h-=t[i];            //更新最优方案结果            if(tans>ans)            {                ans=tans;                for(j=0;j<n;j++)                each[j]=teach[j];            }        }        //输出结果        cout<<each[0];        for(i=1;i<n;i++)        {            cout<<", "<<each[i];        }        cout<<endl;        cout<<"Number of fish expected: "<<ans<<endl;        cout<<endl;    }    return 0;}


原创粉丝点击