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

被折叠的 条评论
为什么被折叠?



