zoukankan      html  css  js  c++  java
  • POJ #1042 Gone Fishing 贪心

    Description


    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.

    Input

    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.

    Output

    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.

    Sample Input

    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 

    Sample Output

    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 

      更多的数据集看这里,尤其注意第一个数据集:链接

      大牛们总结的坑点:坑点一 、坑点二:为什么第一个样例输出是45不是25

    思路


      这道题题目比较饶,我先贴一个题目简介:

      

             来源:北大贪心法讲义

      很显然如果想要最终钓的鱼总数最多,那么每一次钓的鱼应该也要最多。我们选择第 i 个湖为最终能到达的地点,那么贪心法就是每一次钓鱼后对各个湖能收获的鱼的数目进行排序并从中选择一个最优的湖进行钓鱼。

      可能刚开始想会觉得这么做不符合逻辑,因为如果在湖1钓一次、在湖2钓一次后发现此时湖1的鱼更多,那岂不是还得返回湖1再钓一次?但是,实际情况是你可以在湖1钓两次再到湖2钓一次,这两种情况是等价的。

      还有需要注意 OUTPUT 中比较阴险的一句话:

      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.

      即如果所有湖里的鱼都被钓完了,那么 John 可以选择在湖 1 消磨时间、湖2 消磨时间 ... 但是为了防止多解情况发生,优先选择在序号小的湖消磨时间,也就是说多余的时间在湖1消磨。

      我用优先队列存储信息并用堆排序实现贪心,稍微需要注意的地方就是最外层 while 循环最好是只用于判断队列是否为空而不要有其他多余操作,否则很可能有各种微小而致命的 bug 产生。

      

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    using namespace std;
    #define INT_MIN -99999
    
    const int max_n = 25;
    int f[max_n + 1];
    int d[max_n + 1];
    int t[max_n];
    
    struct Node{
        int ff; //单位时间钓鱼数目
        int id; //湖的序号
        friend bool operator < (const Node& a, const Node& b)
        {
            if (a.ff == b.ff) {
                return a.id > b.id;
            }
            return a.ff < b.ff;
        }
    };
    
    int main(void){
        int n;
        int h;
        while (cin >> n && n) {
            cin >> h;
            h = h*12; //转化为单位时间,以 5 mins 为一单位
            for (int i = 1; i <= n; i++)
                cin >> f[i];
            for (int i = 1; i <= n; i++)
                cin >> d[i];
            for (int i = 1; i <= n-1; i++)
                cin >> t[i];
            int ans = INT_MIN; //记录钓鱼的最大数目
            int tt[n+1]; //记录钓鱼最多时在各湖的钓鱼时间,单位是1分钟
            priority_queue<Node> pQueue;
    
            //枚举计算终点在第i个鱼塘的钓鱼最大数目以及时间
            for (int i = 1; i <= n; i++) {
                int sum = 0; //记录钓鱼总数
                int left_time = h; //用于钓鱼的剩余时间,以5mins 为一单位
                int tt2[n+1]; //记录在各湖的钓鱼时间
                if (i > 1) {
                    for (int k = 1; k <= i-1; k++)
                        left_time -= t[k];
                }
                //初始化优先队列以及tt2[]
                memset(tt2, 0, sizeof(tt2));
                for (int j = 1; j <= i; j++) {
                    Node tmp; 
                    tmp.id = j;
                    tmp.ff = f[j];
                    pQueue.push (tmp);
                }
    
                while (!pQueue.empty()) {
                    //贪心选择鱼最多的湖
                    Node tmp = pQueue.top(); pQueue.pop();
                    if (left_time > 0 && tmp.ff > 0) {
                        left_time--;
                        sum += tmp.ff;
                        tt2[tmp.id] += 5;
                        tmp.ff -= d[tmp.id];
                        //更新该湖信息
                        pQueue.push(tmp);    
                    }
                }
                //如果把所有湖的鱼都钓完了,时间还有剩余
                //则选择在湖1消磨时间
                if(left_time > 0)
                    tt2[1] += left_time*5;
    
                if (ans < sum) {
                    ans = sum;
                    memcpy (tt, tt2, sizeof(tt2) );
                }
            } //for() 枚举计算终点在第i个鱼塘的钓鱼最大数目以及时间        
            
            //OUTPUT
            for (int i = 1; i < n; i++) {
                cout << tt[i] << ", ";
            }
            cout << tt[n] << endl;
            cout << "Number of fish expected: " << ans << endl;   
            cout << endl;
        }    
        return 0;
    }
    View Code

      

    ————全心全意投入,拒绝画地为牢
  • 相关阅读:
    2804 最大最小数质因数
    5429 多重背包
    1851 越狱
    Gvim使用
    3622 假期
    4906 删数问题
    2845 排序的代价
    poj 3352
    常用正则表达式汇总
    功能简单例子
  • 原文地址:https://www.cnblogs.com/Bw98blogs/p/8445834.html
Copyright © 2011-2022 走看看