POJ-1042(ZOJ-1161) Gone Fishing

Gone Fishing
Time Limit: 2000MS   Memory Limit: 32768K
     

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

————————————————————高端的分割线————————————————————

前言:这道题目是相当不错的一道题。在黑书的“贪心法”当中,这是第一道例题。值得仔细思考。

题意:有N个湖泊,从第一个开始,一个一个到湖中钓鱼。给出湖泊5分钟能钓到鱼的个数、钓鱼期间每五分钟能钓到鱼减少的个数、从上一个湖到这个湖路上花费的时间,以及总的钓鱼时间,输出一个能钓到最多鱼的方案。方案包括在每个湖中钓鱼的时间,(每次钓鱼都是5分钟的整数倍)。要求花掉所有钓鱼时间,并且在钓鱼数最大的情况下尽量待在靠前的湖泊。

思路:刚开始题意理解错了,没有头绪,后来看了黑书才恍然大悟。【这是多么熟悉的错误!西安邀请赛……QAQ】一开始我以为是给出湖中鱼的总数。其实是5分钟能钓到的鱼数。既然这样,那意味着钓鱼的次数就是突破口。为什么呢?

因为给出了总的时间,全部花完就意味着固定了钓鱼的次数。那么对钓鱼次数进行贪心即可!每次都钓5分钟内出鱼数最多的那个湖!唉……这么简单的题QAQ……

但是问题是:1. 从第一个开始一个湖一个湖钓的问题 2. 尽量待在前面的湖泊

这就需要技巧了。既然我们的贪心方案是每次都钓产量最大的,那么必然先解决掉路上的时间开销。瞬移操作。我们看一组数据:

2
1
0 0
1 1
1

答案是?60 0 0。第二个湖没有必须要去。也就是说,我们需要找到一个方案,在鱼最多的情况下,尽量不去下一个湖。需要枚举“最远‘贪’到哪个湖”,然后维护最优解。

代码如下:

/****************************************/
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 using namespace std;
/****************************************/
int fish_init[30], dec_fish[30], turn[30], cost[30];
struct data
{
	int init, dec, id;
}fish[30];
priority_queue  pq;

bool operator < (const data &a, const data &b)
{
	if(a.init != b.init)
		return a.init < b.init;//能钓到最多鱼的优先出队
	else
		return a.id > b.id;//鱼一样多,靠前的湖泊先出队,例如产量都是0,就死待在第一个湖
}

int fun(int &n, int &time)
{
	int ans = -1;
	data b;
	int tmp[30];
	for(int i = 1; i <= n; i++) {//枚举最远走到哪个湖
		int maxi = 0;
		memset(tmp, 0, sizeof(tmp));
		while(!pq.empty())	pq.pop();
		//清除优先队列
		for(int j = 1; j <= i; j++) {
			pq.push(fish[j]);
		}//注意!是前j个湖泊按照优先序排队,不是所有湖泊
		int t = time - turn[i];///路上的时间全部减去就能瞬移了
		for(; t > 0; t--) {
			b = pq.top();
			pq.pop();
			maxi += b.init;//钓5分钟
			tmp[b.id]++;//在这个湖泊停留5分钟
			b.init -= b.dec;
			if(b.init <= 0) {
				b.init = 0;
			}//鱼量减少,处理一下为负的情况
			pq.push(b);//因为鱼量的减少,优先序发生变化
		}
		if(maxi > ans) {//维护最优解
			ans = maxi;
			for(int j = 1; j <= i; j++) {
				cost[j] = tmp[j];
			}
		}
	}
	return ans;
}

int main()
{
	int n, first = 0;
	while(~scanf("%d", &n), n) {
		int time, ans;
		scanf("%d", &time);
		time *= 12;
		for(int i = 1; i <= n; i++) {
			scanf("%d", &fish[i].init);
			cost[i] = 0;
		}
		for(int i = 1; i <= n; i++) {
			scanf("%d", &fish[i].dec);
			fish[i].id = i;
		}
		turn[1] = 0;
		int t;
		for(int i = 2; i <= n; i++) {
			scanf("%d", &t);
			turn[i] = turn[i-1] + t;//记录的是从第一个湖泊到该湖泊的时间开销
		}//瞬移操作
		ans = fun(n, time);
		if(first)
			puts("");
		first = 1;
		for(int i = 1; i < n; i++)
			printf("%d, ", 5*cost[i]);
		printf("%d\n", 5*cost[n]);
		printf("Number of fish expected: %d\n", ans);
	}
	return 0;
}

在ZOJ的1161,也有这道题,但是WA哭了。再看一下输入数据,肿么多了个1???题目里说了吗?没有!!!神坑。在while(~)前面加上一句

int N;

scanf("%d", &N);
for(int cas = 0; cas < N; cas++)

while(~)

就能过了……

你可能感兴趣的:(ACM-ZOJ,ACM-POJ)