如果没有烘干部分,其实是一道很经典的贪心题,可以直接用优先队列解决,这里不多说。
而这个问题有一个重要性质:设a和b分别为每件衣服洗完衣服的时间,定义a优于b,当且仅当对任意,
,在该“优于”的定义下,存在最优解。(注意这点对于其他题是可能不成立的)
设a表示某个洗衣服的解,b表示某个烘干的解(不考虑洗衣服),则 c,其中 表示一个洗完衣服并烘干的解,可以发现,存在该问题的最优解,满足a和b是对应子问题的最优解。
(注意这里有个误区,就是这两个子问题要分开思考,如果将其看作一个整体,则容易将第二个问题看作,带有起始时间限定的贪心,处理极为棘手。)
有了这一点,考虑最优解,即为:找到一个a的排列p,一个b的排列q,使得 最小,这其实是“广义排序不等式”,和广义矩阵乘法与矩阵乘法的关系一样,它与普通排序不等式具有很多相同性质,其中就包括最小化和最大化的方法。
Code:
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int w[N], d[N];
int l, n, m;
struct Node {
int time, cost;
bool operator < (const Node &b) const {
return time + cost > b.time + b.cost;
}
};
int a[N], b[N];
priority_queue<Node> q;
signed main() {
scanf("%lld%lld%lld", &l, &n, &m);
for (int i = 1; i <= n; i++) scanf("%lld", &w[i]);
for (int i = 1; i <= m; i++) scanf("%lld", &d[i]);
for (int i = 1; i <= n; i++) q.push({0, w[i]});
for (int i = 1; i <= l; i++) {
Node top = q.top(); q.pop();
a[i] = top.time + top.cost;
q.push({a[i], top.cost});
}
while (q.size()) q.pop();
for (int i = 1; i <= m; i++) q.push({0, d[i]});
for (int i = 1; i <= l; i++) {
Node top = q.top(); q.pop();
b[i] = top.time + top.cost;
q.push({b[i], top.cost});
}
sort(a + 1, a + l + 1);
sort(b + 1, b + l + 1, greater<int>());
int ans = 0;
for (int i = 1; i <= l; i++) ans = max(ans, a[i] + b[i]);
printf("%lld\n", ans);
return 0;
}