题意:

John现有h个小时的空闲时间,他打算去钓鱼。钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(John每在一个湖钓完鱼后,他只能走到下一个湖继续钓),John必须从1号湖开始钓起,但是他可以在任何一个湖结束他此次钓鱼的行程。此题以5分钟作为单位时间,John在每个湖中每5分钟钓的鱼数随时间的增长而线性递减。每个湖中头5分钟可以钓到的鱼数用fi表示,每个湖中相邻5分钟钓鱼数的减少量用di表示,John从任意一个湖走到它下一个湖的时间用ti表示。求一种方案,使得John在有限的h小时中可以钓到尽可能多的鱼。

注意:

1)每在一个湖钓完鱼后,他只能走到下一个湖,不能回头。

2)所求的方案可能有多种,但是题目中要求的方案需要满足下面条件:如果存在多种方案,那么选取在第一个湖(钓鱼)停留时间最长的一种,或者在第二个湖·····第N个湖停留时间最长的一种方案。(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. )

 

分析:

1)可以枚举john在哪些湖钓鱼的所有情况,然后,总时间去掉路上花费的时间。剩余的时间,每次选举一个能钓到最多的鱼的湖钓鱼。

2)剩余时间大于0,但是,所有湖能钓到的鱼的个数都为0,此时需要把剩余时间全部加在第一个湖的停留时间中。

3)当钓到的鱼的数量更新时,停留时间列表必须更新。

4)当钓到的鱼的数量与当前所掉的鱼的数量相等时,停留时间列表可能需要更新。

 

代码如下:

#include <iostream>
#define maxn 30

using namespace std;

int num, hour,ans,anst;
int f[maxn],d[maxn],t[maxn],ft[maxn];
int res[maxn] , temp[maxn];
void solve()
{
   t[0] = 0;

   //统计不同的停留方案所需要的停留时间
   for(int i = 1 ; i < num; i ++) t[i] += t[i-1];

   //以5分钟为计量单位的总时间
   int h = hour*12;
   int pos;
   ans  = -1 ;

   for(int k = 0 ; k < num ; k ++)
   {

       anst = 0;

       //ft数组存的是f数组的副本,因为在计算过程中,需要改变ft数组的值;
       //temp数组存储的是当前情况下,在每个湖停留的时间的列表
       for(int i = 0 ; i < num ; i ++) temp[i] = 0 ,ft[i] = f[i];

       //去掉路上的时间,之后钓鱼的总时间
       int th = h - t[k];

       while(th > 0)
       {
          pos = 0;
          for(int j = 0 ; j <= k ; j ++)
          {//找到当前时间,能钓到最大量的鱼,所在的湖
              if(ft[j] > ft[pos])
                pos  = j;
          }
        //等于号不能去掉。如果在pos位置所能钓到的鱼的数量等于0,
        //那么这一个时间单位将没有必要放在pos所在的湖(可能更新到第一个湖上)
         if(ft[pos] <= 0) break;
          anst += ft[pos];//更新鱼数量
          ft[pos] -= d[pos];//更新所能钓到的鱼的数量
          temp[pos] ++;//更新pos位置停留时间信息

          th --;
       }
       //剩余时间全部加在第一个湖上
       if(th > 0) temp[0] += th;

       if(anst > ans)
       {//更新结果值和停留时间列表
           ans = anst;
           for(int i = 0 ; i < num ; i ++) res[i] = temp[i];
       }
       else if(anst == ans)
       {//判断并更新停留时间列表
           bool flag = true;

           for(int i = 0 ; i < num ; i ++)
              if(temp[i] > res[i])
              {
                    flag = false;
                    break;
              }else if(temp[i] < res[i]) break;
           if(!flag)
               for(int i = 0 ; i < num ; i ++) res[i] = temp[i];
       }
   }
}
void print()
{//输出结果
	for(int i = 0 ; i < num - 1 ; i ++) cout<<res[i]*5<<", ";
	cout<<res[num-1]*5<<endl;

	cout<<"Number of fish expected: "<<ans<<endl;
	cout<<endl;
}
int main()
{
	while(cin>>num,num)
	{
	    cin>>hour;
		for(int i = 0 ; i < num ; i ++) cin>>f[i] ;
		for(int i = 0 ; i < num ; i ++) cin>>d[i] ;
		for(int i = 1 ; i < num ; i ++) cin>>t[i] ;

		solve();
		print();
	}
	return 0;
}

  

posted on 2015-06-24 15:31  fqbrighter  阅读(385)  评论(0编辑  收藏  举报