poj 1042 Gone Fishing 贪心

                                                                                         钓鱼

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9   Accepted: 5

Description

有h 个小时给John 钓鱼,共有n 个池塘,1<=h<=16,2<=n<=25。池塘是线性、单向连接
的,按照顺序从1 到n 编号。John 一开始在1 号池塘,ti 表示从i 到i+1 号池塘要花多少个
5 分钟,0 John 在第i 个池塘停留的第一个5 分钟可以钓到fi 条鱼,以后每5 分钟减少di 条,fi>=0,
di>=0。你的任务是确定John 最多能钓到多少条鱼。

Input

有多组数据。每组的第一个行是整数n。第二行是h。
接下来的一行有n 个数fi。下一行有n 个数di。
最后一行是n-1 个数ti。当n=0 时中止。

Output

对于每组数据输出2 行,第一行是在每个池塘停留的时间(分钟),用逗号和空格分割。
下一行是最多能钓到多少条鱼。
如果有多个解,输出在1 号池塘停留时间最长的,即使有时没有鱼可钓。如果还有多个解,
输出在1 号池塘停留时间最长的,依此类推。
两组数据中间输出一行空行。注意:冒号和逗号后面都有一个空格。

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

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;

int fish[30],jian[30],way[30],tmp[30],ans[30];

struct node
{
	int yu,xb,jian,t;
	node(){}
	node(int yu,int xb,int jian,int t):yu(yu),xb(xb),jian(jian),t(t){}
	bool operator < (const node &a)const
	{
		if(yu==a.yu)return xb>a.xb;
		return yu<a.yu;
	}
}tt[30];
priority_queue<node>q;

int main()
{
	int cnt=0;
	int h,lake;
	while(scanf("%d",&lake)!=EOF&&lake)
	{
	    scanf("%d",&h);
	    memset(ans,0,sizeof(ans));
	    h*=60;
	    for(int i=1;i<=lake;i++)
                scanf("%d",&fish[i]);
	    for(int i=1;i<=lake;i++)
                scanf("%d",&jian[i]);
	    int times;
	    way[1]=0;
	    bool flag=false;
	    for(int i=2;i<=lake;i++)
	    {
		scanf("%d",&times);
		way[i]=way[i-1]+times*5;//预处理最远到达第i个湖所花费的时间
	    }
	    int maxn=0;
	    for(int i=1;i<=lake;i++)//遍历到达的湖的范围1-i
	    {
		int lefttime=h-way[i];//剩下的时间就等于总时间减去最远到达湖所花费的时间
		int sum=0;
		for(int j=1;j<=i;j++)
		    q.push(node(fish[j],j,jian[j],0));
		while(lefttime>=5&&q.top().yu>0)
		{
			lefttime-=5;
			node a=q.top();
			q.pop();
			sum+=a.yu;
			a.yu-=a.jian;
			a.t+=5;
			q.push(a);
		}
		if(sum>maxn||!flag)//flag目的是避免下面给出的样例的错误
		{
			flag=true;
			maxn=sum;
			while(!q.empty())
			{
			    ans[q.top().xb]=q.top().t;
			    q.pop();
			}
			if(lefttime>0)ans[1]+=lefttime;//如果时间还有多就全部加给第一个湖
	        }
	        while(!q.empty())q.pop();
	    }
	    for(int i=1;i<=lake;i++)
            {
		if(i==1)printf("%d",ans[i]);
		else printf(", %d",ans[i]);
	    }
	    printf("\nNumber of fish expected: %d\n\n",maxn);
	}
	return 0;
}
/*
3
1
0 0 0
0 0 0
1 2
*/
 

猜你喜欢

转载自blog.csdn.net/qq_40942372/article/details/79984007
今日推荐