Gone fish 1042

//By XieJiang 2017/03/15-2017/03/16
//Gone Fishing 1042
//策略,采取贪心:每次选取鱼最多的池塘
//贪心+枚举,也是参考了别人的博客CIA明白的,DP也可以,无奈水平实在太渣
#include<iostream>
using namespace std;
int n, h;//记录池塘数量,允许小时数
int fish[26][26], ftime[26][26], f[26], d[26], t[25];//最终每个池塘的捕鱼数,池塘的花费的时间,池塘初始鱼数,下降比例,池塘之间的时间
int ans[26];//贪心下,总的鱼数量

int main() {
    int i, j, tn, th, emp, maxfish, maxschme, tf[26];
    cin >> n;
    while (n > 0) {
        cin >> h;
        h = h * 12;
        memset(ans, 0, sizeof(ans));
        memset(ftime, 0, sizeof(ftime)); memset(fish, 0, sizeof(ftime));
        for (i = 1; i <= n; i++)//输入每个池塘的鱼数量
            cin >> f[i];
        for (i = 1; i <= n; i++)//输入每个池塘每五分钟下降的鱼数量
            cin >> d[i];
        for (i = 1; i < n; i++)//输入相邻间隔池塘的路上花费的时间
            cin >> t[i];
        //开始枚举(从1到n)
        for (tn = 1; tn <= n; tn++) {
            //目前要垂钓的湖的范围是1-tn
            //计算路上花费的时间
            th = h;
            for (i = 1; i < tn; i++)
                th -= t[i];
            //将f[26]复制到一个临时的数组内,用以一次枚举过程
            for (i = 1; i <= tn; i++)
                tf[i] = f[i];
            //模拟垂钓过程,结束条件:湖空或者时间用光,同时剩余时间放入第一个湖。
            emp = 0;
            while (th > 0 && emp < tn) {
                //选择鱼最多的池塘
                maxfish = 1;
                for (i = 2; i <= tn; i++)
                    if (tf[i] > tf[maxfish])
                        maxfish = i;
                if (tf[maxfish] <= 0)
                    break;
                //表示下一次将选择这个池塘,将相关信息去除。
                ans[tn] += tf[maxfish];
                tf[maxfish] -= d[maxfish];
                ftime[tn][maxfish]++;
                th--;
                //判断空的湖
                if (tf[maxfish] <= 0)
                    emp++;
            }
            //将剩余未完时间归于湖1
            if (th > 0)
                ftime[tn][1] += th;

        }
        //选出鱼数量最多的方案
        maxschme = 1;
        for (i = 2; i <= n; i++) 
            if (ans[i] > ans[maxschme])
                maxschme = i;
        //输出结果
        for (i = 1; i < n; i++)
            cout << ftime[maxschme][i]*5 << ", ";
        cout << ftime[maxschme][n]*5 << endl;
        cout << "Number of fish expected: " << ans[maxschme] << endl;
        cin >> n;
        if (n != 0)
            cout << endl;
    }
}
原文地址:https://www.cnblogs.com/1996313xjf/p/6558986.html