HUST算法实践__POJ1042

问题描述

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.

输入

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.

输出

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.

算法思想

枚举+贪心

分析题目要求,我们发现时间由两部分组成:钓鱼的时间和走路的时间。

我们可以枚举John最后的位置。由于最后的位置确定了,且题目要求只能单向移动,所以走路的时间也就确定了,剩余的时间就是钓鱼的时间。此时,问题就可以用贪心算法来较为简单地解决了。

实现细节
  1. 将每个池塘展开,写入一个新的数组中。注意:每5分钟钓鱼量<=0时变不再写入新数组。
  2. 假设当前枚举的John最后位置为i, 将新数组中位置在1i中的元素加入到一个优先队列中。借助优先队列实现贪心选取。
  3. 若时间还有剩余,将全部时间加到第一个池塘上。

代码实现

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;

class FISH{
public:
	int cnt;
	int pos;
public:
	FISH(int cnt=0,int pos=0):cnt(cnt),pos(pos){}
	bool operator <(const FISH& f)const{
		if(cnt!=f.cnt) return cnt<f.cnt;
		return pos>f.pos;
	}
};

priority_queue<FISH>q;
int n,h;
int p[26],d[26],t[26];
int np[5200],last[26];
int a;
int cnt=1;
int sum[26];
int M;

void init(){
	for(int i=1;i<=n;i++){
		int temp=p[i];
		for(int j=1;j<=h;j++){
			if(temp<=0) break;
			np[cnt++]=temp;
			temp-=d[i];
		}
		last[i]=cnt-1;
	}
}

int main(){
	while(1){
		cnt=1;
		memset(sum,0,sizeof(sum));
		M=0;
		while(!q.empty()) q.pop();
		cin>>n;
		if(!n) break;
		cin>>h;
		h*=12;
		for(int i=1;i<=n;i++) cin>>p[i];
		for(int i=1;i<=n;i++) cin>>d[i];
		for(int i=2;i<=n;i++){
			cin>>a;
			t[i]=t[i-1]+a;
		}
		init();
		for(int i=1;i<=n;i++){		//枚举终点 
			while(!q.empty()) q.pop();
			int _M=0;
			int _sum[26];
			memset(_sum,0,sizeof(_sum));
			int fish_h=h-t[i];
			if(fish_h<=0) continue;
			int pos=1;
			for(int j=1;j<=last[i];j++){
				while(j>last[pos]) pos++;
				q.push(FISH(np[j],pos));
			}
			while(fish_h){
				if(q.empty()){
					_sum[1]+=fish_h;
					break;
				}
				_M+=q.top().cnt;
				_sum[q.top().pos]++;
				q.pop();
				fish_h--;
			}
			if(_M>M){
				memcpy(sum,_sum,sizeof(sum));
				M=_M;
			}
			else if(_M==M){
				bool flag=0;
				for(int i=1;i<=n;i++){
					if(_sum[i]>sum[i]){
						flag=1;
						break;
					}
					else if(_sum[i]<sum[i]){
						flag=0;
						break;
					}
				}
				if(flag){
					memcpy(sum,_sum,sizeof(sum));
					M=_M;
				}
			}
		}
		cout<<5*sum[1];
		for(int i=2;i<=n;i++) cout<<", "<<5*sum[i];
		cout<<"\nNumber of fish expected: "<<M<<"\n\n";
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值