• NYOJ 30 Gone Fishing(枚举+贪心)


    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 
    
    思路:枚举从lake_1到lake_i路程花费的时间,然后从总时间减去,为了方便,以5 minutes为一个单位,进行计算;
    每次选取能够掉得鱼的最大值maxn,k次相加集可得到sum(需要注意的一点是尽可能lake的id值小,枚举时,可正可逆,但代码需要做下改动)。
    代码如下:
    正序枚举
     1  
     2 #include<stdio.h>
     3 struct lake
     4 {
     5     int fish;
     6     int decrease;
     7     int time;
     8     int stay;
     9 } K[30];
    10 int main()
    11 {
    12     int k_fish[30],k_stay[30],T[30];//T[i]表示路上花去时间
    13     int sum,maxn,flag,i,j,p,t,n,hour,k;
    14     while(scanf("%d",&n)&&n!=0)
    15     {
    16         flag=0;
    17         scanf("%d",&hour);
    18         hour *=12;
    19         for(i=0;i<n;i++){
    20             scanf("%d",&K[i].fish);
    21             k_fish[i]=K[i].fish;}
    22         for(i=0;i<n;i++)
    23             scanf("%d",&K[i].decrease);
    24         for(i=1;i<n;i++)
    25         scanf("%d",&K[i].time); K[0].time=0;//time表示从0到i
    26         for(i=0;i<=n-1;i++)//题目信息改变时(尽可能在后面的湖停留),可逆置或者该**标注的地方
    27         {
    28             if(!i)
    29             T[i] = K[i].time;
    30             else T[i]=T[i-1]+K[i].time;
    31             sum=0;
    32              for(t=0;t<n;t++){
    33                K[t].fish=k_fish[t];
    34                   K[t].stay=0;}
    35             for(k=1;k<=hour-T[i];k++)
    36             {   maxn=0;
    37                 for(j=0;j<=i;j++)
    38                 if(maxn<K[j].fish) { maxn=K[j].fish; p=j;}//定取鱼位置
    39 
    40                 sum+=maxn;
    41                 if(maxn>0){
    42                 K[p].fish-=K[p].decrease;
    43                 K[p].stay++;}
    44                 else K[0].stay++;//多余时间加在lake_1
    45 
    46             }
    47             if(sum>flag) {//**sun>=flag
    48                     flag=sum;
    49             for(j=0;j<n;j++)
    50                 k_stay[j]=K[j].stay*5;//保存最优题解
    51             }
    52        }
    53        for(j=0;j<n-1;j++)
    54             printf("%d, ",k_stay[j]);
    55             printf("%d
    ",k_stay[n-1]);
    56        printf("Number of fish expected: %d
    
    ",flag);
    57     }
    58     return 0;
    59 }
    60         
    View Code

     逆置枚举:

     1  
     2 #include<stdio.h>
     3 struct lake
     4 {
     5     int fish;
     6     int decrease;
     7     int time;
     8     int stay;
     9 } K[30];
    10 int main()
    11 {
    12     int k_fish[30],k_stay[30],T[30];//T[i]表示路上花去时间
    13     int sum,maxn,flag,i,j,p,t,n,hour,k;
    14     while(scanf("%d",&n)&&n!=0)
    15     {
    16         flag=0;
    17         scanf("%d",&hour);
    18         hour*=60;
    19         for(i=0;i<n;i++){
    20             scanf("%d",&K[i].fish);
    21             k_fish[i]=K[i].fish;}
    22         for(i=0;i<n;i++)
    23             scanf("%d",&K[i].decrease);
    24         for(i=1;i<n;i++)
    25         scanf("%d",&K[i].time); K[0].time=0;//time表示从0到i
    26          T[0]=0;
    27         for(i=0;i<n;i++)
    28         {
    29             K[i].time *= 5;
    30             if(!i)
    31             T[i] = K[i].time;
    32             else T[i]=T[i-1]+K[i].time;
    33         }
    34         for(i=n-1;i>=0;i--)//逆置
    35         {
    36             sum=0;
    37              for(t=0;t<n;t++){
    38                K[t].fish=k_fish[t];
    39                   K[t].stay=0;}
    40             for(k=1;k<=(hour-T[i])/5;k++)
    41             {   maxn=0;
    42                 for(j=0;j<=i;j++)
    43                 if(maxn<K[j].fish) { maxn=K[j].fish; p=j;}//定取鱼位置
    44 
    45                 sum+=maxn;
    46                 if(maxn>0){
    47                 K[p].fish-=K[p].decrease;
    48                 K[p].stay++;}
    49                 else K[0].stay++;//多余时间加在lake_1
    50 
    51             }
    52             if(sum>=flag) {
    53                     flag=sum;
    54             for(j=0;j<n;j++)
    55                 k_stay[j]=K[j].stay*5;//保存最优题解
    56             }
    57        }
    58        for(j=0;j<n-1;j++)
    59             printf("%d, ",k_stay[j]);
    60             printf("%d
    ",k_stay[n-1]);
    61        printf("Number of fish expected: %d
    
    ",flag);
    62     }
    63     return 0;
    64 }
    65         
    View Code


     

  • 相关阅读:
    《梦段代码》阅读笔记03
    用户场景
    冲刺!
    冲刺!
    周总结8
    冲刺!
    冲刺!
    PMS权限管理和鉴权过程
    PMS构造函数以及apk如何扫描
    PMS的学习之旅
  • 原文地址:https://www.cnblogs.com/luoshuihanbing/p/3293468.html
Copyright © 2020-2023  润新知