首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一个很简单的贪心有关问题,看下为什么会超时

2013-12-10 
一个很简单的贪心问题,看下为什么会超时啊Gone FishingTime Limit: 2000MSMemory Limit: 32768KTotal Subm

一个很简单的贪心问题,看下为什么会超时啊
Gone Fishing
Time Limit: 2000MSMemory Limit: 32768K
Total Submissions: 27311Accepted: 8086

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



10 1 
2 5 



10 15 20 17 
0 3 4 3 
1 2 3 


10 15 50 30 
0 3 4 3 
1 2 3 


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 

Source
East Central North America 1999



#include <stdio.h>
#include <stdlib.h>
#include<string.h>

int h;
int fi[30];
int di[30];
int ti[30];
int num;

void regret()
{
    int tem=0;
    int max=0;


    int remain;
    int fish[30]={0};
    int anstime[30]={0};
    int temtime[30]={0};
    int i,j;
    int fishnum;
    int cur;
    for(i=0;i<num;i++)
    {
        remain=h;
       fishnum=0;
       for(j=0;j<=i;j++)
         remain-=ti[j];
       memcpy(fish,fi,sizeof(fi));
       while(remain)
       {
           cur=0,tem=fish[0];
           for(j=1;j<=i;j++)
           {
               if(tem<fish[j])
               {
                   tem=fish[j];
                   cur=j;
               }
           }

        fish[cur]-=di[cur];
        fishnum+=tem;
        if(fish[cur]<0)
           fish[cur]=0;
        temtime[cur]++;
        remain--;
       }
       if(fishnum>max)
       {
           max=fishnum;
           memcpy(anstime,temtime,sizeof(anstime));
       }
       memset(temtime,0,sizeof(temtime));
    }
    for(i=0;i<num-1;i++)
    printf("%d, ",anstime[i]*5);
    printf("%d\n",anstime[num-1]*5);
    printf("Number of fish expected: %d\n",max);
    memset (anstime,0,sizeof(anstime)) ;
    }

int main()
{
    int i;
    scanf("%d",&num);
    while(num)
    {
        scanf("%d",&h);
        h=h*12;
        for(i=0;i<num;i++)
          scanf("%d",&fi[i]);
        for(i=0;i<num;i++)
          scanf("%d",&di[i]);
        for(i=1;i<num;i++)
           scanf("%d",&ti[i]);
        regret();
        memset (fi,0,sizeof(fi)) ;
        memset (di,0,sizeof(di)) ;
        memset (ti,0,sizeof(ti)) ;
        scanf("%d",&num);
    }
    return 0;
}


[解决办法]
如下修改可在ZO上AC:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

int h;
int fi[30];
int di[30];
int ti[30];
int num;

void regret()
{
    int tem=0;
    int max=0;
    int remain;
    /*删int fish[30]={0};*/
    int anstime[30]={0};
    int temtime[30]={0};
    int i,j;
    int fishnum;
    int cur;


    for(i=0;i<num;i++)
    {
       memset(temtime, 0, sizeof(temtime));/*加*/
       remain=h;
       fishnum=0;
       for(j=0;j<=i;j++)
         remain-=ti[j];
       /*删 memcpy(fish,fi,sizeof(fi));*/
       if (remain < 0) continue; /*加. 如果没有此行,当remain<0导致下面死循环而超时*/
       while(remain)
       {
           cur=0,tem=0; /*改 tem=fish[0]*/
           for(j=0;j<=i;j++)/*改 for(j=1;j<=i;j++)*/
           {
               if(tem<fi[j]-di[j]*temtime[j]) /*改 if(tem<fish[j])*/
               {
                   tem=fi[j]-di[j]*temtime[j];
                   cur=j;
               }
           }

           /*删 fish[cur]-=di[cur];*/
           fishnum+=tem;
           /*删 
           if(fish[cur]<0)
              fish[cur]=0;*/
           temtime[cur]++;
           remain--;
       }
       if(fishnum>max)
       {
           max=fishnum;
           memcpy(anstime,temtime,sizeof(anstime));
       }
       /*memset(temtime,0,sizeof(temtime));*/
    }
    for(i=0;i<num-1;i++)
    printf("%d, ",anstime[i]*5);
    printf("%d\n",anstime[num-1]*5);
    printf("Number of fish expected: %d\n",max);
    memset (anstime,0,sizeof(anstime)) ;
}

int main()
{
    int i;
    int t0, t1; /*加*/
    scanf("%d", &t0); /*加*/
    while (t0--) /*加*/
    { /*加*/
      t1 = 0; /*加*/
      scanf("%d",&num);
      while(num)
      {
          if (t1++) printf("\n"); /*加*/
          scanf("%d",&h);
          h=h*12;
          for(i=0;i<num;i++)
            scanf("%d",&fi[i]);
          for(i=0;i<num;i++)
            scanf("%d",&di[i]);
          for(i=1;i<num;i++)
             scanf("%d",&ti[i]);
          regret();
          memset (fi,0,sizeof(fi)) ;
          memset (di,0,sizeof(di)) ;
          memset (ti,0,sizeof(ti)) ;
          scanf("%d",&num);
      } /*加*/
      if (t0 > 0) printf("\n"); /*加*/
    }
    return 0;
}

热点排行