P1717钓鱼

本文介绍了一种使用C++手写实现优先队列的方法,并通过一个具体的应用场景——资源调度问题来展示其实现过程及效果。该实现利用了大根堆的数据结构,通过不断调整堆元素的位置来维护堆的性质。

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

优先队列,手写

#include<iostream>
#include<cstdio>
using namespace std;
int n,T,k,mx;
struct HP{
    int f,d,t;
}hp[30],nod[30],nd;
void mat(int t)
{
    int now=t;
    int tp=2*now;
    while(tp<=k)
    {
        if(tp<k && hp[tp].f<hp[tp+1].f) tp=tp+1;
        if(hp[tp].f>hp[now].f)
        {
            nd=hp[now];
            hp[now]=hp[tp];
            hp[tp]=nd;
            now=tp;
        }
        else break;
    }
}
int main()
{
    scanf("%d%d",&n,&T);
    int tim=T*60/5;//向下取整 
    for(int i=1;i<=n;i++) scanf("%d",&nod[i].f);
    for(int i=1;i<=n;i++) scanf("%d",&nod[i].d);    
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&nod[i].t);
        nod[i].t+=nod[i-1].t;//nod[3].t
    }
//  for(int i=1;i<=n;i++) cout<<nod[i].t<<" ";//检查时间加和是否正确 
    for(k=1;k<=n;k++)              //枚举最终走到哪个池塘
    {
        int sm=0;
        int st=tim-nod[k].t;
        if(st<=0) continue;
        for(int j=1;j<=k;j++) hp[j]=nod[j];
        for(int i=1;i<=k/2;i++) mat(i); //建堆
//      cout<<hp[1].f<<endl;//这里检查大根堆是否正确 
        while(st && hp[1].f)
        {
            sm+=hp[1].f;
            hp[1].f-=hp[1].d;
            mat(1); 
            st--;
        }
        if(sm>mx) mx=sm;
    }   
    cout<<mx<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值