NYOJ Gone Fishing 貪心策略

Gone Fishing
時間限制:3000 ms | 內存限制:65535 KB
難度:5
描述
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個湖,John可以在這個n個湖中穿行,ti*5表示第i個湖到i+1湖之間需要的時間間隔.fi表示第一次在每個湖中能釣到的魚數,每在第i個湖釣一次 ,該湖中能掉到的魚數就會減少di。需要求出John在時間60*h內就能釣到最多的魚數目,以及在每個湖上話費的時間。

解題思路
很明顯的貪心策略,假設John最後所在的湖爲 x,我們可以發現在路上花費的時間60*h-tx,這些時間是完全用在釣魚上,而不用考慮湖之間的距離,所以每一次釣魚都可以在第0個湖到第x個湖之間選擇每次可以釣到最多的湖進行釣魚。只要枚舉0~n能釣到魚的數量,最後求最大值就可以啦。哈哈哈。

上代碼


#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;



int main()
{
    int num;
    while (~scanf("%d", &num) && num != 0) {
        int h;
        cin >> h;
        int f[25];
        int d[25];
        int inter[25];
        int i = 0;
        for (; i < num; i++) {
            cin >> f[i];

        }
        //cout << "fi" << endl;
        /*for (i = 0; i < num; i++) {
            cout << f[i] << " ";
        }*/
    //  cout << endl;
        for (i = 0; i < num; i++) {
            cin >> d[i];
        }
        /*cout << "di" << endl;
        for (i = 0; i < num; i++) {
            cout << d[i] << " ";
        }*/
        //cout << endl;
        int in;
        inter[0] = 0;
        for (i = 1; i < num; i++) {
            cin >> in;
            inter[i] = inter[i - 1] + in;
        }
        /*cout << "inter" << endl;
        for (i = 0; i < num; i++) {
            cout << f[i] << " ";
        }*/
        //cout << endl;
        int time = h * 60;
        int fish_time[25];
        for (i = 0; i < num; i++) {
            fish_time[i] = time - inter[i] * 5;
        }

        int ff[25];
        i = 0;
        int maxi = 0;
        int fish[25];
        for (i = 0; i < num; i++) {
            int w;
            for (w = 0; w < num; w++) {
                ff[w] = f[w];
            }
            fish[i] = 0;
            int time_left = fish_time[i] / 5;
            int k = 0;
            for (; k < time_left; k++) {
                int j = 0;
                int maxfi = 0;
                int maxi = 0;
                for (; j <= i; j++) {
                    if (maxfi < ff[j]) {
                        maxfi = ff[j];
                        maxi = j;
                    }
                }
                fish[i] += maxfi;
                ff[maxi] = ff[maxi] - d[maxi];
            }
        }
        i = 0;
        int max = 0;
        int lacki = 0;
        for (; i < num; i++) {
            if (max < fish[i]) {
                max = fish[i];
                lacki = i;
            }
        }
        int lack_time[25];
        for (i = 0; i < num; i++) {
            lack_time[i] = 0;
        }
        i = 0;
        for (; i < num; i++) {
            ff[i] = f[i];
        }

        int time_left = fish_time[lacki] / 5;
        int k = 0;
        for (; k < time_left; k++) {
            int j = 0;
            int maxfi = 0;
            int maxi = 0;
            for (; j <=lacki; j++) {
                if (maxfi < ff[j]) {
                    maxfi = ff[j];
                    maxi = j;
                }
            }
            ff[maxi] = ff[maxi] - d[maxi];
            lack_time[maxi] += 5;
        }

        for (i = 0; i < num; i++) {
            if (i == 0) {
                cout << lack_time[i];
            }
            else
            {
                cout  << ", "<< lack_time[i];
            }           
        }
        cout <<endl<<"Number of fish expected: "<< max<<endl;
                 cout<<endl;
    }
    return 0;
}

最後感謝一番
大概有一年多沒有做過題了,本來就很水,現在更水。但是算法又相當重要,所以還是趕緊學習,刷一刷題吧。另外,我看了此題的標程,感覺自己的代碼就是shi啊。說說標程的優點吧。值得學習。
1.封裝性很好,不會像我一樣把所有部分都一股腦的塞進main中,而是將要處理的部分封裝到函數中,這樣代碼就很簡介,漂亮,並且容易修改。
2.函數命名合理(哎,不多說,英語不好程序猿的都懂)。
3.使用庫函數,往往比自己寫的速度要快很多。
4.思路很清晰。
所以,任重而道遠啊!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章