UVA 757 Gone Fishing
阿新 • • 發佈:2017-06-09
desc mes dex ins 心算 相同 https names min
題目鏈接: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 notice6 // 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