程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA 757 Gone Fishing(貪心 + 暴力)

UVA 757 Gone Fishing(貪心 + 暴力)

編輯:C++入門知識

 Gone Fishing 


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 lake i 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 fi ( ), then a line of n integers di ( ), and finally, a line of n - 1 integers ti (). 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

題意:單位時間為5分鐘,給定n個池塘,和h個小時。每個池塘有兩個屬性f,d,f代表池塘初始可以釣到的魚,d代表每個單位時間池塘可以釣到的魚會減少d。在給定每個池塘之間路程所需要的單位時間。要求出在h小時內,最多能釣到多少魚以及在每個池塘花掉的時間,人一開始在第一個池塘。


思路:暴力 + 貪心。要掉到更多的魚。就要盡可能利用每一分每一秒。所以人從第一個池塘開始肯定是往後走不回頭的。


所以我們只要枚舉每一個區間,1到1,1到2,1到3。。。1到n的情況,每個區間可以用的時間為:總時間 - 路程花費時間。 找出其中最大的情況。在每種情況在用貪心去求。貪心的策略是:一個個單位時間去考慮,每次去找最大可以釣到的魚數。這樣到最後一定是釣得最多的魚。

代碼:


 

#include <stdio.h>
#include <string.h>
using namespace std;

int n, h, t[30], Max;
struct Lake {
    int f;
    int d;
    int t;
} l[30], ll[30], out[30];

int main() {
    int bo = 0;
    while (~scanf("%d", &n) && n) {
	scanf("%d", &h);
	h *= 12; Max = -1;
	for (int i = 0; i < n; i ++)
	    scanf("%d", &ll[i].f);
	for (int i = 0; i < n; i ++)
	    scanf("%d", &ll[i].d);
	for (int i = 0; i < n - 1; i ++)
	    scanf("%d", &t[i]);
	for (int i = 0; i < n; i ++) {
	    int sum = 0;
	    int time = h;
	    memset(l, 0, sizeof(l));
	    for (int j = 0; j < n; j ++)
		l[j] = ll[j];
	    for (int j = 0; j < i; j ++)
		time -= t[j];
	    while (time > 0) {
		int Maxx = 0, Max_v = 0;
		for (int k = 0; k <= i; k ++){//找最大可以釣到的魚
		    if (l[k].f > Maxx) {
			Maxx = l[k].f;
			Max_v = k;
		    }
		}
		sum += l[Max_v].f;
		if (l[Max_v].f - l[Max_v].d >= 0)
		    l[Max_v].f -= l[Max_v].d;
		else 
		    l[Max_v].f = 0;
		l[Max_v].t += 5;
		time --;
	    }
	    if (Max < sum) {
		Max = sum;
		for (int i = 0; i < n; i ++) {
		    out[i].t = l[i].t;
		}
	    }
	}
	if (bo ++) printf("\n");//注意輸出格式
	for (int i = 0; i < n - 1; i ++)
	    printf("%d, ", out[i].t);
	printf("%d\n", out[n - 1].t);
	printf("Number of fish expected: %d\n", Max);
    }	
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved