贪心问题

问题: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

回答:题意是说在笔直的路边有一些池塘,从左到右编号为1,2...n。John共有H个小时的空余时间,他希望能钓到尽量多的鱼。他从池塘1出发,向右走,有选择的在一些池塘边停留一定的时间钓鱼,现已经测出John从第i个湖到第i+1个湖走路需要的时间5*Ti分钟,还测出在第i个湖边停留,第一个5分钟可以钓到的鱼Fi,以后每5分钟能钓到的鱼的数量减少Di.为简化问题假定没有其它因素会影响到他钓鱼的数量,要求出John能钓到最多鱼的方案。
主要思路如下:
把钓5分钟鱼称为钓一次鱼。首先枚举John需要走过的池塘的数目X,即假设他从湖泊1走到湖泊X,则路上花去的时间T=sum(Ti) i=1...X-1.在这个前提下,可以认为John能从一个池塘"瞬间转移"到另一个池塘,即在任意一个时刻都可以从湖泊1到湖泊X中任选一个钓一次鱼(很重要)。现在采用贪心策略,每次选择鱼最多的湖泊钓一次鱼。对于每个池塘来说,由于在任何时候鱼的数目只和John在该池塘里钓鱼的次数有关,和钓鱼的总次数无关,所以这个策略是最优的。假设一共允许钓k次鱼,那么每次在N个池塘中选择鱼最多的一个钓。总的时间复杂度为O(kn^2)。

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int t[30];
int T[30];
int tim[30];

struct node {
    int f;
    int d;
    int index;
}num[30];

struct cmp {
    bool operator ()(const node &a,const node &b) {
        if (a.f!=b.f)
            return a.f<b.f;
        return a.index>b.index;
    }
};

int main() {

    int n,h;
    bool first(true);
    while (cin>>n&&n) {
        cin>>h;
        int i;
        for (i=0;i<n;++i) {
            cin>>num[i].f;
            num[i].index = i;
        }
        for (i=0;i<n;++i)
            cin>>num[i].d;
        for (i=0;i<n-1;++i)
            cin>>t[i];

        int total = h*12;

        int output(-1);
        memset(T,0,sizeof(T));

        int x;
        for (i=0;i<n;++i) {
            int temp;
            int j;
            priority_queue<node,vector<node>, cmp> cq;
            for (temp = 0,j=0;j<i;++j)
                temp+=t[j],cq.push(num[j]);
            cq.push(num[i]);
            x = total - temp;

            int r = 0;
            memset(tim,0,sizeof(tim));
            node top;
            while (x>0&&!cq.empty()) {
                top = cq.top();
                cq.pop();
                --x;
                r+=top.f;
                tim[top.index]+=5;
                if (top.f>top.d)
                    top.f-=top.d;
                else  top.f = 0;

                cq.push(top);

            }

            if (r>output) {
                output = r;
                int k;
                for (k=0;k<n;++k)
                    T[k] = tim[k];

            }
        }

        i = 0;

        while (i< n-1)
            cout<<T[i++]<<", ";
        cout<<T[n-1]<<endl;
        cout<<"Number of fish expected: "<<output<<endl;
        cout<<endl;
        first = false;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/benchao/p/4533381.html