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.
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
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; }