UVA 757 Gone Fishing
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 757 Gone Fishing相关的知识,希望对你有一定的参考价值。
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=9&problem=698&mosmsg=Submission+received+with+ID+19496827
利用枚举和贪心来计算。枚举在每个湖stop fishing的情况,在每种情况下,贪心算法得出能钓得最多鱼的数目。因为题目要求: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. 所以在贪心算法中,如果有多个湖可钓的鱼的数目相同,选最先遇到的那个湖。不要觉得这样就可以了。当一种情况结束之后,如果可以得到的鱼的总数有相同的,还要继续比较两种情况下,从第一个湖开始,选择在lake 1停留之间长的情况。代码如下:
1 //============================================================================ 2 // Name : test.cpp 3 // Author : 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream> 10 #include <math.h> 11 #include <stdio.h> 12 #include <cstdio> 13 #include <algorithm> 14 #include <string.h> 15 #include <cstring> 16 #include <queue> 17 #include <vector> 18 #include <functional> 19 #include <cmath> 20 #define SCF(a) scanf("%d", &a) 21 #define IN(a) cin>>a 22 #define FOR(i, a, b) for(int i=a;i<b;i++) 23 typedef long long Int; 24 using namespace std; 25 26 int main() 27 { 28 int n, h; //n lakes, h hours = h*12 "5 minutes" 29 int fish[30]; //initial fish number 30 int decrease[30]; //decrese unit 31 int time[30]; //walk time 32 int tcase = 0; 33 int stay[30]; //time that stay at lake i 34 int take[30]; //time to take for people walk from lake 0 to lake i; 35 while (SCF(n) && n) 36 { 37 if (tcase >= 1) 38 printf("\n"); 39 SCF(h); 40 FOR(i, 0, n) 41 SCF(fish[i]); 42 FOR(i, 0, n) 43 SCF(decrease[i]); 44 FOR(i, 0, n - 1) 45 SCF(time[i]); 46 FOR(i, 0, n) 47 stay[i] = 0; 48 49 take[0] = 0; 50 FOR(i, 1, n) 51 take[i] = take[i - 1] + time[i-1]; 52 53 int maxFish = 0; 54 int left[30]; //record available fish for each lake 55 int cfish = 0; //total fish that can get for current strategy 56 int cstay[30]; //record the time stay at each lake 57 int ctime = h * 12; 58 59 FOR(i, 0, n) //fishing destination is lake i 60 { 61 ctime = h * 12; 62 cfish = 0; 63 64 ctime -= take[i]; 65 FOR(j, 0, n) 66 { 67 left[j] = fish[j]; 68 cstay[j] = 0; 69 } 70 71 while (ctime > 0) 72 { 73 int cmax = 0; 74 FOR(j, 0, i+1) 75 { 76 if (left[j] > left[cmax]) 77 cmax = j; 78 } 79 cfish += left[cmax]; 80 left[cmax] = max(0, left[cmax] - decrease[cmax]); 81 cstay[cmax] += 5; 82 ctime -= 1; 83 } 84 85 if (cfish > maxFish) 86 { 87 maxFish = cfish; 88 FOR(j, 0, n) 89 stay[j] = cstay[j]; 90 } 91 else if (cfish == maxFish) 92 { 93 FOR(j, 0, n) 94 { 95 if (cstay[j] > stay[j]) 96 { 97 FOR(z, j, n) 98 stay[z] = cstay[z]; 99 break; 100 } 101 else if (cstay[j] < stay[j]) 102 break; 103 } 104 } 105 } 106 107 printf("%d", stay[0]); 108 FOR(i, 1, n) 109 printf(", %d", stay[i]); 110 printf("\n"); 111 printf("Number of fish expected: %d\n", maxFish); 112 tcase++; 113 } 114 return 0; 115 }
刚开始没有写91行else if的情况,以为在贪心算法中就已经考虑优先选择靠前的湖就可以了,结果一直wrong answer。加上之后才AC。
以上是关于UVA 757 Gone Fishing的主要内容,如果未能解决你的问题,请参考以下文章