Gone Fishing POJ - 1042 ( 贪心)

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

做这个题目真是一波三折,刚开始的就想直接用优先队列,把每次每个湖的平均增量(钓鱼一次的数量除以时间)和湖序号加入队列,最开始的时候,第一次的平均增量是要加路途上的用时,后面如果在i湖钓过鱼,就把i及之前 湖平均增量(此时不用算路途时间了)和湖序号加入队列,,,我这样想着,然后敲好代码运行,发现不行,,想着为啥不行,我自己又发现了自己的问题,算湖平均增量的时候这样是不太对的,照我这样,后面路途远但钓鱼数量多的湖会因为第一次的路途用时长而不会被选到,,,然后我就继续改这个平均增量,把它换成在某个湖剩余时间可钓到鱼的数量除以时间,,,改了之后,发现还是不行,,又想了想,发现可以把路途单独考虑,h减去路途用时,在剩下的h在这些湖中选择钓鱼最多的,,

这样终于可以得到正确的结果了,,
新的问题又来了,,我没有看懂那个在每个湖时间是怎么算的,这也很坑了,太可怕了,,我就只能去看看别人的,

最后一点儿小细节,就是题目要求在钓鱼相同的情况下选择序号小的湖,为此我自己定义了比较函数,,到此我终于可以提交ac了,

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
#define N 30
#define PI 3.14159265358979323
#define INF 0x3f3f3f3f
#define MOD 100000007
#define ll long long
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
int n,h,a[N];
int f[N],d[N],t[N],st[N];
int pass[N],path[N];
int vis[N];
int fish,mfish,reh;
typedef pair < int ,int > P;
struct cmp {
	bool operator ()( P p1, P p2) { // 由于在可以钓相同的鱼数量 情况下,要选择序号小的湖,所以要自己定义比较函数 
		if ( p1.first == p2.first )
		return p1.second > p2.second ;
	
		return p1.first <p2.first ;
		
	}
	
};
priority_queue< P ,vector< P> , cmp  > que;
void solve (int hour,int num ){
	P te,re;
	int i;
	fish=0;
	while ( !que.empty() ) que.pop() ;
	
	for ( i=0;i<num;i++ ){
			//cout<<te.first <<"   ";
		te.first =f[i];
		te.second =i;
		que.push(te) ;
		
	}
	//cout<<endl;
	while ( hour>0&&!que.empty()  ){
		int lake;
		te=que.top() ;
		que.pop() ;
		lake=te.second ;
		if ( f[lake ]-d[lake ]*(vis[lake ])<=0 ) continue ;
		
		if ( hour<5 ) break;

		hour-=5;
		
		fish+=f[lake ]-d[lake ]*vis[lake ];
		vis[lake ]++;
		
			te.first = f[lake]-vis[lake]*d[lake];
			te.second =lake;
			if ( te.first >0 )
			que.push ( te );
		
	}
	
	if ( mfish<fish ){
		mfish=fish;
		reh=hour; // 记录剩余的时间 
		//h=hour;
		for ( i=0;i<n;i++ )
		pass[i]=vis[i];
	}
}
//template < class T> void read ( T &x ){	char c;int sign=1;x=0;while ( c=getchar(),(c>'9'||c<'0')&&c!='-') ;if ( c=='-' ){sign=-1;}else x=c-'0';while ( c=getchar(),c<='9'&&c>='0' ){x=x*10+c-'0';}x=x*sign;}
int main ( )
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int i,fg;
	while ( cin>>n,n ){
		cin>>h;
		mfish=-1;
		for ( i=0;i<n;i++ ){
			cin>>f[i];
		}
		for ( i=0;i<n;i++ ){
			cin>>d[i];
		}
		memset ( st,0,sizeof(st));
		memset ( path,0,sizeof ( path));
		memset ( pass,0,sizeof ( pass));
		for ( i=0;i<n-1;i++ ){
			cin>>t[i];
			st[i+1]=st[i]+t[i];
		}
		h*=60;
		reh=0;
		for ( i=1;i<n+1;i++ )
		{
			memset ( vis,0,sizeof(vis));
			solve ( h-st[i-1]*5,i);
		}
		for ( i=n-1;i>=0;i-- )
		{
			path[i]=pass[i+1]+path[i+1];
			//cout<<path [i]<<"  "<<i<<endl;
			
		}
		//cout<<endl;
		cout<<(pass[0]*5+reh);  // 把剩余的时间加到第一个湖所用时间里,,这个时间 也很坑, 
		
		
		for ( i=1;i<n;i++ ){
		
			cout<<", "<<pass[i]*5;
		}
		//cout<<", "<<pass[i]*5;
		cout<<"\nNumber of fish expected: "<<mfish<<"\n"<<endl;
		
	}
	return 0;
}
	

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值