首页 > 其他分享 >UVa 757 / POJ 1042 / East Central North America 1999 Gone Fishing (枚举&贪心&想法题&优先队列)

UVa 757 / POJ 1042 / East Central North America 1999 Gone Fishing (枚举&贪心&想法题&优先队列)

时间:2023-04-12 13:05:01浏览次数:46  
标签:1042 757 fish lake number expected fi line America


757 - Gone Fishing

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=698

http://poj.org/problem?id=1042

John is going on a fishing trip. He has h hours available ( 

), and there are n lakes in the area ( 

) 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 

, the number of 5-minute intervals it takes to travel from lakei to lake i + 1 is denoted ti ( 

). 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 ( 

), 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 ( 

). 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  f

i ( 

), then a line of n

 integers  d

i ( 

), and finally, a line of n

 - 1 integers  t

i ( 

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


21
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, 5Number of fish expected: 31

240, 0, 0, 0
Number of fish expected: 480

115, 10, 50, 35
Number of fish expected: 724



首先枚举去哪些湖钓鱼。

由于每个湖中鱼的数量只与钓了多长时间有关,与什么时候钓无关。因此我们可以一次将移动的时间全部扣除,这样每时每刻我们都可以选择鱼数量最多的湖。


完整代码:

/*UVa: 0.059s*/
/*POJ: 110ms,176KB*/

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int d[30], f[30], cost[30], h, n, ans[30], maxi;

struct lake
{
	int fi;
	int id;
	bool operator < (const lake p) const
	{
		if (fi == p.fi)
			return id > p.id;
		return fi < p.fi;
	}
} la[30], tt;

void solve()
{
	priority_queue<lake>Q;
	int i, j, t, gf, tp[30];
	maxi = -1;
	for (i = 1; i <= n; i++)
	{
		while (!Q.empty()) Q.pop();
		memset(tp, 0, sizeof(tp));
		gf = 0;
		t = h * 12 - cost[i];
		for (j = 1; j <= i; j++)
		{
			la[j].fi = f[j], la[j].id = j;
			Q.push(la[j]);
		}
		for (j = 0; j < t; j++)
		{
			tt = Q.top();
			Q.pop();
			gf += tt.fi;
			tp[tt.id]++;
			if (tt.fi - d[tt.id] > 0)
				tt.fi -= d[tt.id];
			else
				tt.fi = 0;
			Q.push(tt);
		}
		if (gf > maxi)
		{
			maxi = gf;
			for (j = 1; j <= n; j++)
				ans[j] = tp[j] * 5;
		}
	}
}

int main(void)
{
	bool flag = 0;
	int i;
	cost[1] = 0;
	while (scanf("%d", &n), n)
	{
		memset(ans, 0, sizeof(ans));
		if (flag) putchar('\n');
		flag = 1;
		scanf("%d", &h);
		for (i = 1; i <= n; i++)
			scanf("%d", &f[i]);
		for (i = 1; i <= n; i++)
			scanf("%d", &d[i]);
		for (i = 2; i <= n; i++)
			scanf("%d", &cost[i]);
		for (i = 3; i <= n; i++)
			cost[i] = cost[i] + cost[i - 1];
		solve();
		printf("%d", ans[1]);
		for (i = 2; i <= n; i++)
			printf(", %d", ans[i]);
		printf("\n");
		printf("Number of fish expected: %d\n", maxi);
	}
}



标签:1042,757,fish,lake,number,expected,fi,line,America
From: https://blog.51cto.com/u_5535544/6185484

相关文章

  • UVA - 757 Gone Fishing 贪心+枚举
    题目大意:有n个湖泊,每个湖泊最初的5分钟能钓到f条鱼,每五分钟减少d条鱼,鱼的数目不能小于d也不能为负数,求在h小时能钓到的鱼的最大数目和在每个池塘带了多少分钟解题思路:一个个枚举,如果用总时间减去到达另一个湖泊的时间的话,就表示它可以在两个湖泊随意行走了,然后在这些时间找到优解,并......
  • PAT Basic 1042. 字符统计
    PATBasic1042.字符统计1.题目描述:请编写程序,找出一段给定文字中出现最频繁的那个英文字母。2.输入格式:输入在一行中给出一个长度不超过1000的字符串。字符串由......
  • 1757. 可回收且低脂的产品
    题目链接:https://leetcode.cn/problems/recyclable-and-low-fat-products/题目描述: 由题目可知,我们需要查找低脂可回收的产品编号,用一行就可以搞定:SELECTproduct_i......
  • 756~757 Session 细节2,细节3销毁
    4.细节:1.当客户端关闭后,服务器不关闭,两次获取session是否为同一个默认情况下。不是2.客户端不关闭,服务器关闭后,两次获取的Session是同一个吗?不......
  • NYOJ-757-期末考试
    期末考试时间限制:1000ms|内存限制:65535KB难度:2描述马上就要考试了,小T有许多作业要做,而且每个老师都给出来了作业要交的期限,如果在规定的期限内没交作业就会扣......
  • 2020-2021 ACM-ICPC Latin American Regional
    K-Keylogger就是你可以显然的发现一个\(O(n^3)\)级别的动态规划。设\(dp_{i,j}\)表示第\(i\)位密码,现在按的键是\(j\)的答案。然后发现矩阵的每一行是单调递......
  • CF757G Can Bash Save the Day? (复健 Day 1)
    先差分为\(Q(r)-Q(l-1)\),\(Q(i)=\sum_{j=1}^{i}\operatorname{dis}(p_j,x)\)。树上在线路径优先考虑点分树,先想询问怎么做,我们记\(f_i\)为点分树上\(i\)点子树内所......
  • luogu P2757 [国家集训队]等差子序列
    Link题解降智了。。。首先我们不需要关心\(Len\)是多少,只需要找到长度为\(3\)的等差子序列就行了。然后就枚举中点\(mid\),看看存不存在\(l<mid<r\)使得\(a_{mi......
  • CP1042 阶乘的尾数
    本想做个小题,没想到牵扯出了大数阶乘(悲本质上还是极限俺的做法:#include<stdio.h>intchange(intm);intmain(){ inta[20001]; intm,u; inttemp,digit,n,i,j=......
  • [20210429更新]软件方法(下)分析和设计 第8章 连载
    墙上挂了根长藤,长藤上面挂铜铃《长藤挂铜铃》;词:元庸,曲:梅翁(姚敏),唱:逸敏,1959您在阅读《软件方法》时如果发现错误,欢迎通过微信umlchina2告知。如果作者认为有道理,决定在下一次......