1. 程式人生 > 其它 >1042:Gone Fishing,考點:貪心策略

1042:Gone Fishing,考點:貪心策略

原題:http://bailian.openjudge.cn/practice/1042/

描述

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.

樣例輸入

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 

解法

思路:貪心+列舉。列舉最終停下來的湖,將方案分為n類,每類方案的走路時間就是確定的,在每類方案中找最優解,保留所有方案中最優的結果。

坑:一開始的魚初值設為-1,所以在純釣魚時間小於等於0(也就是沒有釣魚)的時候也要進行處理,不能直接break;

搜到的一些很有用的測試用例:http://poj.org/showmessage?message_id=162286

AC程式碼如下:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <memory>
 6 using namespace std;
 7 const int MAXN = 26;
 8 int F[MAXN][200];
 9 struct stay {
10     int lake;
11     int time;
12     int fish;
13     stay(int l, int t, int f) :lake(l), time(t), fish(f) {}
14     bool operator <(const stay A)const {
15         if (fish == A.fish)
16             return lake < A.lake;
17         return fish > A.fish;
18     }
19 };
20 int main() {
21     int n;//n個湖
22     int numcase = 0;
23     while (cin >> n) {
24         if (n == 0)break;
25         int h;//總時間
26         cin >> h;
27         h = h * 12;//以5分鐘為一個單位
28         int f[MAXN], d[MAXN], t[MAXN];
29         for (int i = 0; i < n; i++)
30             cin >> f[i];
31         for (int i = 0; i < n; i++)
32             cin >> d[i];
33         for (int i = 0; i < n - 1; i++)
34             cin >> t[i];
35         memset(F, 0, sizeof(F));
36         for (int i = 0; i < n; i++) {
37             F[i][0] = f[i];
38             for (int j = 1; j < h; j++) {
39                 if (F[i][j - 1] - d[i] < 0)
40                     break;
41                 F[i][j] = F[i][j - 1] - d[i];
42             }
43         }
44         int fish = -1;
45         int result[MAXN];
46         memset(result, 0, sizeof(result));
47         for (int lake = 0; lake < n; lake++) {//列舉停下來的湖為i
48             int walktime = 0;
49             for (int j = 0; j < lake; j++)
50                 walktime += t[j];
51             int k = h - walktime;//純釣魚時間
52             if (k <= 0) {
53                 fish = max(fish, 0);
54                 break;
55             }
56             vector<stay>find_best;
57             for (int i = 0; i <= lake; i++)
58                 for (int j = 0; j < k; j++)
59                     find_best.push_back(stay(i, j, F[i][j]));
60             sort(find_best.begin(), find_best.end());
61             int sumfish = 0;
62             for (int i = 0; i < k; i++)
63                 sumfish += find_best[i].fish;
64             if (sumfish > fish) {
65                 fish = sumfish;
66                 memset(result, 0, sizeof(result));
67                 for (int i = 0; i < k; i++)
68                     result[find_best[i].lake] ++;
69             }
70             else if (sumfish == fish) {
71                 int temp[MAXN];
72                 memset(temp, 0, sizeof(temp));
73                 for (int i = 0; i < k; i++)
74                     temp[find_best[i].lake]++;
75                 bool needChange = false;
76                 for (int i = 0; i < n; i++) {
77                     if (temp[i] > result[i]) {
78                         needChange = true;
79                         break;
80                     }
81                     else if (temp[i] < result[i])
82                         break;
83                 }
84                 if (needChange)
85                     memcpy(result, temp, sizeof(temp));
86             }
87         }
88         if (numcase != 0)cout << endl;
89         for (int i = 0; i < n; i++)
90         {
91             if (i == 0)cout << result[i] * 5;
92             else cout << ", " << result[i] * 5;
93         }
94         cout << endl;
95         cout << "Number of fish expected: " << fish << endl;
96         numcase++;
97     }
98     return 0;
99 }