gone fishing 贪心

来源:互联网 发布:2015年淘宝网店数量 编辑:程序博客网 时间:2024/06/07 14:26

这道题刚开始的时候完全看不懂,题目英文,那个每次钓鱼减少量不是每五分钟就减少一次,而是每在这个鱼塘钓鱼之后钓鱼量会减少一次。至于每次减少的量题目倒是给出了。由于这道题给出的数据问题,所以当时间还有剩余的时候,将剩下的时间都算到第一个鱼塘中去,也许是人性化一点吧。至于题目到底有没有提到这一点,实在是不太清楚,毕竟英文翻译成中文总归有些差别

这道题的解法在我看来有点暴力的感觉

因为用的是枚举:每一种情况都考虑进去了。

1:第一个池塘钓鱼(在里面钓不一定钓了鱼)

2:1,2个池塘钓鱼

3: 1, 2, 3,个池塘钓鱼

。。。。

n: 1, 2, 3, 4,....n, 个池塘钓鱼


h = h*12; 因为题目中的时间均是按照五分钟来考虑的,所以只要把h换成5的倍数就好了,这样就相当于次数加减,方便的多


每一次情况中,我们要考虑的是这次的最大钓鱼量,取所有的最大的就可以了。

每一次的时候都要将路上的时间减掉,因为在哪里钓完了,就不动了。

比方是第m次

1,2,3,。。。m个池塘

开始的时候,找这M个中最大钓鱼量,之后,因为这个鱼塘钓了一次,所有需要把他的每次钓鱼量减掉fish m。之后再在所有中找到最大的钓鱼量,一共要找 h1 次(这个h是已经减去了路上的时间)。这次中的sum = max1+max2+max3+.....max h1,  和记录的max比较,哪个大就取哪个。当然需要有两个数组来记录那些找的最大的池塘的钓鱼数,一个原始,一个不停的变动。



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.

输入

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.

输出

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.

样例输入

2110 12 524410 15 20 170 3 4 31 2 34410 15 50 300 3 4 31 2 30

样例输出

45, 5Number of fish expected: 31240, 0, 0, 0Number of fish expected: 480115, 10, 50, 35Number of fish expected: 724


#include<stdio.h>#include<string.h>int main(){int n, h, f1[28], f2[28], de[28], t1[28], t2[28], t3[28], i = 0, j = 0, k = 0, f = 0;while (scanf ("%d", &n), n){if (f)printf ("\n");f = 1; scanf ("%d", &h);h*=12;//all timefor (i = 1; i <= n; i++)scanf ("%d", &f1[i]);for (i = 1; i <= n; i++)scanf ("%d", &de[i]);for (i = 2; i <= n; i++)    scanf ("%d", &t1[i]);t1[1] = 0;int now , pre = -1;for (i = 1; i <= n; i++){now = 0;h-=t1[i]; for (j = 1; j <= n; j++) f2[j] = f1[j];memset(t2, 0, sizeof(t2));for (j = 1; j <= h; j++){int max = 1;for (k = 2; k <= i; k++){if (f2[k] > f2[max])max = k;}if (f2[max]<=0) break;now+=f2[max];t2[max]+=5;f2[max]-=de[max];}if (now > pre){pre = now;for (j = 1, k = 0; j <= n; j++){t3[j] = t2[j];k += t2[j];}t3[1] += h*5-k;}}for (i = 1; i <= n; i++){if (i!=1)printf (", ");printf ("%d", t3[i]);}printf ("\nNumber of fish expected: %d\n", pre);}return 0;}