题目描述
你现在要洗 l ll 件衣服。你有 n nn 台洗衣机和 m mm 台烘干机。由于你的机器非常的小,因此你每次只能洗涤(烘干)一件衣服。
第 i ii 台洗衣机洗一件衣服需要 wi w_iwi 分钟,第 i ii 台烘干机烘干一件衣服需要 di d_idi 分钟。
请问把所有衣服洗干净并烘干,最少需要多少时间?假设衣服在机器间转移不需要时间,并且洗完的衣服可以过一会再烘干。
输入
输入文件的第一行三个整数 l ll、n nn 和 m mm。
第二行 n nn 个整数 wi w_iwi。
第三行 m mm 个整数 di d_idi。
输出
一行一个整数,表示所需的最少时间。
样例输入
1 1 1
1200
34
样例输出
1234
提示
对于 10% 10\%10% 的数据,l=1 l = 1l=1;
对于另外 20% 20\%20% 的数据,l,n,m≤10 l, n, m \leq 10l,n,m≤10;
对于另外 30% 30\%30% 的数据,l≤1000,n,m≤100 l \leq 1000, n, m \leq 100l≤1000,n,m≤100;
对于 100% 100\%100% 的数据,l≤106,n,m≤105 l \leq 10 ^ 6, n, m \leq 10 ^ 5l≤106,n,m≤105。
#include<bits/stdc++.h>
#define Max(x,y) ((x) > (y) ? (x) : (y))
#define Min(x,y) ((x) < (y) ? (x) : (y))
using namespace std;
const int maxn = 1e6 + 5;
int l,n,m;
long long w[maxn],d[maxn],ans;
long long t[maxn],t2[maxn];
long long cntw[maxn],cntd[maxn];
struct Node{
long long t,id;
Node(long long _t=0,long long _id=0):t(_t),id(_id){}
bool operator <(const Node &a)const {
return t > a.t;
}
};
priority_queue<Node>q1,q2,q3;
template<class T>inline void read(T &x){
x = 0;bool flag = 0;char ch = getchar();
while(!isdigit(ch)) flag |= ch == '-',ch = getchar();
while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
if(flag) x = -x;
}
template<class T>void putch(const T x){
if(x > 9) putch(x / 10);
putchar(x % 10 | 48);
}
template<class T>void put(const T x){
if(x < 0) putchar('-'),putch(-x);
else putch(x);
}
void file(){
freopen("laundry.in","r",stdin);
freopen("laundry.out","w",stdout);
}
void readdata(){
read(l);read(n);read(m);
for(int i = 1;i <= n; ++ i) read(w[i]);
for(int i = 1;i <= m; ++ i) read(d[i]);
sort(w + 1,w + n + 1);
sort(d + 1,d + m + 1);
}
void work1(){
put(w[1] + d[1]);
}
void work(){
long long cn=1,cm=1;
q1.push(Node(w[1],cn));
for(int i = 1;i <= l; ++ i){
Node x = q1.top();
q1.pop();
t[i] = x.t;
q1.push(Node(x.t + w[x.id],x.id));
if(x.id == cn){
cn++;
if(cn <= n)q1.push(Node(w[cn],cn));
}
}
q2.push(Node(d[cm],cm));
for(int i = l;i >= 1; -- i){
Node x = q2.top();
q2.pop();
if(x.id == cm) {
cm++;
if(cm <= m)q2.push(Node(d[cm],cm));
}
t[i] += x.t;
//这里的t[i]不一定就是真实的洗衣时间,但对答案无影响
//最小化等待时间
x.t += d[x.id];
q2.push(x);
ans = max(ans,t[i]);
}
put(ans);
}
int main(){
// file();
readdata();
if(l == 1) work1();
else work();
return 0;
}