HDU 6000 优先队列+快速IO

本文介绍了一个关于衣物清洗的问题,通过使用优先队列实现贪心算法来解决如何在有限的洗衣机和甩干机数量下,最小化全部衣物清洗完成所需的时间。文章详细解释了算法思路并提供了完整的代码实现。

题意

有L件衣服,N个洗衣机,M个甩干机,每个洗衣机和甩干机都有独立的处理时间,问洗完这些衣服最少需要多少时间。

题解

利用优先队列贪心地去做,也算是模拟吧,把衣服扔到洗衣机中,取出来加到一个数组中(加到队列中会超时)。然后倒着取出来这个数组中的元素(因为我们要保证洗衣机最后洗完的元素优先加到甩干机中,这样才能保证截止时间最短)。最后在所有衣服处理的截止时间中取最大值就可以了。

代码

#include<bits/stdc++.h>
#define LL long long
#define UP(i,l,h) for(LL i=l;i<h;i++)
#define DOWN(i,h,l) for(LL i=h-1;i>=l;i--)
#define W(t) while(t)
#define INF 0x3f3f3f3f
#define MEM(a,b) memset(a,b,sizeof(a))
#define BUF 50001000
using namespace std;
char Buf[BUF],*buf=Buf;
struct Node
{
    LL ed,len;
    bool operator < (const Node b) const
    {
        return ed+len>b.ed+b.len;
    }
};
LL ans;
LL big[1001000];
int getint()
{
    char c;
    for(; *buf<48; buf++);
    int x=0;
    W(*buf>47)
    {
        x=x*10+(*buf-'0');
        buf++;
    }
    return x;
}

void solve()
{
//    priority_queue<LL> big;        //大在顶
    priority_queue<Node> wash;
    priority_queue<Node> dry;
    MEM(big,0);
    int l,n,m;
    l=getint();
    n=getint();
    m=getint();
    UP(i,0,n)
    {
        Node nd;
        nd.len=getint();
        nd.ed=0;
        wash.push(nd);
    }
    UP(i,0,m)
    {
        Node nd;
        nd.len=getint();
        nd.ed=0;
        dry.push(nd);
    }
    UP(i,0,l)
    {
        Node tp=wash.top();
        wash.pop();
        tp.ed+=tp.len;
//        big.push(tp.ed);
        big[i]=tp.ed;
        wash.push(tp);
    }
    ans=0;
    DOWN(i,l,0)
    {
        LL tp=big[i];
        Node nd=dry.top();
        dry.pop();
        ans=max(ans,tp+nd.ed+nd.len);
        nd.ed+=nd.len;
        dry.push(nd);
    }
}

int main()
{
    fread(Buf,1,BUF,stdin);
    int t;
    t=getint();
    int ks=1;
    W(t--)
    {
        solve();
        printf("Case #%d: %I64d\n",ks++,ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值