Gone fishing

Gone fishing

成绩10开启时间2018年02月1日 星期四 20:25
折扣0.8折扣时间2018年12月29日 星期六 20:25
允许迟交关闭时间2018年12月29日 星期六 20:25

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

Source

East Central North America 1999

 测试输入关于“测试输入”的帮助期待的输出关于“期待的输出”的帮助时间限制关于“时间限制”的帮助内存限制关于“内存限制”的帮助额外进程关于“{$a} 个额外进程”的帮助
测试用例 1以文本方式显示
  1. 2↵
  2. 1↵
  3. 10 1↵
  4. 2 5↵
  5. 2↵
  6. 4↵
  7. 4↵
  8. 10 15 20 17↵
  9. 0 3 4 3↵
  10. 1 2 3↵
  11. 4↵
  12. 4↵
  13. 10 15 50 30↵
  14. 0 3 4 3↵
  15. 1 2 3↵
  16. 0↵
以文本方式显示
  1. 45, 5↵
  2. Number of fish expected: 31↵
  3. 240, 0, 0, 0↵
  4. Number of fish expected: 480↵
  5. 115, 10, 50, 35↵
  6. Number of fish expected: 724↵
1秒64M0
刘汝佳的黑书


写法一:用优先队列

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

struct node
{
	int fish;
	int mark;
	friend bool operator < (node n1, node n2)
	{
		if (n1.fish != n2.fish)
			return n1.fish<n2.fish;
		else
			return n1.mark>n2.mark;
	}
}lakes[26];



int t[26];
int d[26];
int ans[26];
int h;
int n;
int cas;
int maxn;
int times;
int main()
{
	//freopen("1.txt", "r", stdin);
	//cin >> n >> h;
	while (scanf("%d", &n) != -1)
	{
		if (n == 0) break;
		cin >> h;
		for (int i = 1; i <= n; i++)
		{

			cin >> lakes[i].fish;
			lakes[i].mark = i;
		}
		maxn = 0;
		for (int i = 1; i <= n; i++)
		{
			cin >> d[i];
		}
		for (int i = 1; i <= n - 1; i++)
		{
			cin >> t[i];
		}
		if (cas > 0)
			cout << endl;
		cas++;
		for (int end = 1; end <= n; end++)
		{
			priority_queue <node> q;

			times = h * 12;
			int num = 0;
			int tmp[26] = {0};
			for (int i = 1; i <= end; i++) q.push(lakes[i]);

			for (int i = 1; i<end; i++) times -= t[i];

			while (times > 0)
			{

				node lake = q.top();
				q.pop();
				if (lake.fish > 0)
				{
					
					int lake_mark = lake.mark;
					if (d[lake_mark] == 0)
					{
						num += lake.fish*times;
						tmp[lake_mark] += times;
						times = 0;
						break;
					}
					times -= 1;
					num += lake.fish;
					lake.fish -= d[lake_mark];
					tmp[lake_mark]++;
					q.push(lake);
				}
				else break;
			}
			if (num > maxn)
			{
				maxn = num;
				for (int i = 1; i <= n; i++) ans[i] = tmp[i];
				if (times) ans[1] += times;
			}
		}
		if (!maxn)ans[1] += h * 12;
		for (int i = 1; i <= n; i++)
		{
			if (i == n)
				cout << ans[i]*5;
			else
				cout << ans[i]*5 << ',' << ' ';
		}
		cout << endl;
		cout << "Number of fish expected: " << maxn << endl;
	}
	return 0;
}
写法二:不用优先队列
#include<iostream>
#define Maxn 30
using namespace std;

int n, h;
int f[Maxn];
int d[Maxn];
int t[Maxn];
int ans[Maxn];
int cas;
int main()
{
	//freopen("1.txt", "r", stdin);
	while (~scanf("%d", &n) && n)
	{
		cin >> h;
		h *= 12;
		for (int i = 1; i <= n; i++)
			cin >> f[i];
		for (int i = 1; i <= n; i++)
			cin >> d[i];
		for (int i = 1; i < n; i++)
			cin >> t[i];
		int maxn = 0;
		int times;
		if (cas > 0)
			cout << endl;
		cas++;
		for (int end = 1; end <= n; end++)//枚举终点
		{
			times = h;
			int num = 0;//每次终点不同的鱼量
			int tmp[Maxn] = { 0 };//临时存储不同终点情况下每个湖的鱼量
			int ft[Maxn] = { 0 };//临时存储一下1~end湖中的鱼
			for (int i = 1; i <= end; i++)
				ft[i] = f[i];
			for (int i = 1; i < end; i++)//先去除走路的时间
				times -= t[i];
			int k;
			
			while (times>0)
			{
				k = 1;
				for (int j = 1; j <= end; j++)
				{
					if (ft[j] > ft[k])
						k = j;
				}
				if (ft[k] > 0)
				{
					num += ft[k];
					ft[k] -= d[k];
					tmp[k]++;
					times--;
				}
				else break;
			}
			if (num > maxn)
			{
				maxn = num;
				for (int i = 1; i <= n; i++) ans[i] = tmp[i];
				if (times) ans[1] += times;
			}
		}
		for (int i = 1; i <= n; i++)
		{
			if (i == n)
				cout << ans[i] * 5;
			else
				cout << ans[i] * 5 << ',' << ' ';
		}
		cout << endl;
		cout << "Number of fish expected: " << maxn << endl;
	}

	return 0;
}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水之积也不厚,则其负大舟也无力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值