Problem Description
Mr.Panda is about to engage in his favourite activity doing laundry!
He’s brought L indistinguishable loads of laundry to his local
laundromat, which has N washing machines and M dryers.The ith washing
machine takes Wi minutes to wash one load of laundry, and the ith
dryer takes Di minutes to dry a load of laundry. At any point in time,
each machine may only be processing at most one load of laundry. As
one might expect, Panda wants to wash and then dry each of his L loads
of laundry. Each load of laundry will go through the following steps
in order:
1. A non-negative amount of time after Panda arrives at the laundromat, Panda places the load in an unoccupied washing machine i.
2. Wi minutes later, he removes the load from the washing machine, placing it in a temporary holding basket (which has unlimited space)
3. A non-negative amount of time later, he places the load in an unoccupied dryer j
4. Dj minutes later, he removes the load from the dryer Panda can instantaneously add laundry to or remove laundry from a machine. Help
Panda minimize the amount of time (in minutes after he arrives at the
laundromat) after which he can be done washing and drying all L loads
of laundry!
Input
The first line of the input gives the number of test cases, T. T test
cases follow. Each test case consists of three lines. The first line
contains three integer L, N, and M. The second line contains N
integers W1,W2,…,WN representing the wash time of each wash machine.
The third line contains M integers D1,D2,…,DM representing the dry
time of each dryer.
Output
For each test case, output one line containing “Case #x: y”, where x
is the test case number (starting from 1) and y is the minimum time it
will take Panda to finish his laundry. limits∙1≤T≤100. ∙1≤L≤106. ∙1≤N,M≤105. ∙1≤Wi,Di≤109.
Sample Input
2
1 1 1
1200
34
2 3 2
100 10 1
10 10
Sample Output
Case #1: 1234
Case #2: 12
思路
首先说题意,一个人要去洗衣房洗衣服。
这个人有L
件衣服,洗衣房有n
台洗衣机和m
台烘干机。首先题目第一行给出这三个数,接下来有两行,第一行每个数WiWi代表第i
个洗衣机洗衣服时间需要w
,然后一行每个数DiDi代表第i
个烘干机烘干衣服所需要的时间。
我们可以用一个优先队列来维护,洗衣机洗衣服的时间,让花费时间小的先出队。先处理出每件衣服洗完的最小时间t[i]
,在处理晚某件衣服后,然后入队的时候加上洗这个衣服所用的时间(具体请看实现过程).
当我们把t[i]
都处理出来时,现在面临的问题是烘干,我们和洗衣服的时候一样,烘干也是用优先队列,小的先出队,然后我们的贪心策略是在烘干的时候,让最后洗完的衣服用烘干时间最小的洗衣机,这样求出洗衣服+烘干衣服的最大值就是答案。数据很大,需要用到long long
代码
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
typedef long long ll;
const ll N=1e6+20;
struct node
{
ll x,val;//洗处理衣服的时间和当前时间
node() {}
node(ll _x,ll _val)
{
x=_x;
val=_val;
}
bool friend operator < (node a,node b)
{
return a.val>b.val;//使花费时间小的先出队
}
} a[N];
ll t[N],L,n,m;//t[i]代表第i个衣服最早的洗完时间
int main()
{
ll T,x,kase=1;
scanf("%lld",&T);
while(T--)
{
priority_queue<node>q;
scanf("%lld%lld%lld",&L,&n,&m);
for(ll i=0; i<n; i++)
{
scanf("%lld",&x);
q.push(node(x,x));
}
for(ll i=0; i<L; i++)
{
node now=q.top();
q.pop();
t[i]=now.val;//求出第i件衣服的最早洗完时间
q.push(node(now.x,now.x+now.val));//把洗这件衣服的时间算进去
}
while(!q.empty())q.pop();//清空队列
for(ll i=0; i<m; i++)
{
scanf("%lld",&x);
q.push(node(x,x));
}
ll ans=0;
for(ll i=L-1; i>=0; i--)
{
node now=q.top();
q.pop();
ans=max(ans,t[i]+now.val);//洗衣服时间+烘干衣服时间最大的
q.push(node(now.x,now.x+now.val));
}
printf("Case #%lld: %lld\n",kase++,ans);
}
return 0;
}