poj -- 1042 Gone Fishing(枚举+贪心)

题意:

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;
}

  

时间: 2024-08-08 15:54:53

poj -- 1042 Gone Fishing(枚举+贪心)的相关文章

POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)

Gone Fishing Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 30281   Accepted: 9124 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 reachab

NYOJ 30 &amp;&amp; POJ 1042 Gone Fishing(枚举+贪心)

[题目链接]:Click here~~ [题目大意]: 一个人去钓鱼,在一条单向路上的旁边有n个湖,并且从湖i到湖i+1需要ti的时间,每个湖里面有fi条鱼,每钓一次鱼,鱼会减少di条.在给定时间T内,问如何才能使钓的鱼最多,并记录在各个湖上停留的时间. [解题思路] 此题细节处理好多麻烦,一定要认真看清题意,POJ WA了无数遍,纠结一天.参考了别人的题解,思路如下: 首先须注意的一点是,John只能向前走,返回的话只会增加John在路上的时间,因而减少他钓鱼的时间.因此此题解题步骤如下: 1

POJ 1042 Gone Fishing#贪心

(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=30; int n,h,H;//H:记录原本有多少小时的时间:h:贪心的时候,防止H被修改 int res[N],RES[N];//res[]:贪心的时候保存结果:RES[]:用于记录最终结果 int maxn,sum;//maxn:保存最终结果,即捕到的鱼最大值:sum:每轮贪心

贪心/poj 1042 Gone Fishing

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 struct node 5 { 6 int d; 7 int fish; 8 int ans; 9 }; 10 node a[30],b[30]; 11 int t[30]; 12 int n,h,sum; 13 int main() 14 { 15 scanf("%d",&n); 16 while (n!=0) 17 { 18

POJ 1042 Gone Fishing

题意:一个人要在n个湖中钓鱼,湖之间的路径是单向的,只能走1->2->3->...->n这一条线路,告诉你每个湖中一开始能钓到鱼的初始值,和每钓5分钟就减少的数量,以及湖之间的距离,问用h小时最多钓多少鱼.鱼的数量不会增加,而且如果不钓鱼的话鱼的数量不会减少,如果有多个答案,输出在小号的湖上花费时间最多的答案. 解法:贪心.枚举在前i个湖里钓鱼,那么走的路程就是一定的,用总时间减去走过的时间,剩下的时间每5分钟为一个单位,选鱼最多的湖钓,然后更新湖里鱼的数量.据说dp也可以做,大概

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

poj 2586 Y2K Accounting Bug (贪心)

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9632   Accepted: 4806 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.

POJ 1328、Radar Installation 贪心

jQuery的属性操作非常简单,下面以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 添加属性 $('a').attr('href', 'http://www.jquery.com') 添加多个属性 $('a').attr({'href':'http://www.jquery.com', 'title':'jquery.com'}) 获取属性 $('a').attr('href') class属性

POJ 3069 Saruman&#39;s Army(贪心)

Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3069 Appoint description:  System Crawler  (2015-04-27) Description Saruman the White must lead his army along a straight path from