pku 1042 Gone Fishing

本文介绍了一种使用贪心算法解决钓鱼比赛问题的方法。通过枚举不同钓鱼地点并计算钓鱼时间,采用优先队列实现最优分配策略,最终得出最大钓鱼数量及各位置钓鱼次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

贪心做法,思想主要是枚举需要去几个地方钓鱼,   选定了地方后,直接把在路上的时间减去就是钓鱼上所画时间了。接下来按哪个地方鱼多九分配给哪个地方就行,不用考虑路程时间了。

这样做的原因是  如果你先在1处钓鱼,然后  某个时间去了x,  然后再次回到1,这样的话和一开始就在1多钓几次鱼的效果一样,而且前者还多花了路程上的时间。   枚举去前n个地方,然后在1处掉,掉完后去2,然后去3...  直到到n处为止。

 

具体的可以参照黑书。

 

比较好笑的是,题目的要求是Insert a blank line between cases.结果却是要求每次输出后都要求输出空行

 

代码:

 

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值