Gym 101206 B Wash

本文介绍了一种使用优先队列求解衣物清洗与烘干最少时间的算法。通过合理分配洗衣机和烘干机的工作,确保了整体流程的最优化。算法首先计算洗衣服所需最短时间,再匹配烘干过程,最终得出最小总耗时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gym 101206 B Wash

题意

L L L 件衣服, n n n 台洗衣机,每台洗衣机洗一件衣服需要 w [ i ] w[i] w[i] 的时间

m m m 台烘干机,每台烘干机烘干一件衣服需要 d [ i ] d[i] d[i] 的时间

问最少需要多少时间洗完所有衣服


思路

  • 洗衣服的时间肯定越快越好

  • 所以只需要用优先队列即可以找到,洗完所有衣服所需的最少时间

  • int cnt = 0;
    priority_queue<pli, vector<pli>, greater<pli>> q;
    for (int i = 1; i <= n; i++)
        q.push(make_pair(w[i], w[i]));
    while (k--) {
        pli x = q.top();
        q.pop();
        a[++cnt] = x.first;
        q.push(make_pair(x.first + x.second, x.second));
    }
    
  • 烘干衣服的时间肯定是最后洗完的衣服放洗衣速度最快的洗衣机洗

  • 可以将烘干机看成一台台有开始和结束时间的烘干机,然后我们同上方用优先队列即可处理得到

  • 我们需要将洗衣时间和烘干时间配对,那么耗时最多和最少的配对既可以

  • for (int i = 1; i <= m; i++)
        q.push(make_pair(d[i], d[i]));
    k = cnt;
    cnt = 0;
    while (k--) {
        pli x = q.top();
        q.pop();
        b[++cnt] = x.first;
        q.push(make_pair(x.first + x.second, x.second));
    }
    LL ans = 0;
    for (int i = 1; i <= cnt; i++)
        ans = max(ans, a[i] + b[cnt + 1 - i]);
    

代码

#include <bits\stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
const int maxn = 1e5 + 5;
int w[maxn], d[maxn];
LL a[maxn * 10];
LL b[maxn * 10];
int main()
{
    int t, g = 0;
    scanf("%d", &t);
    while (t--) {
        int k, n, m;
        scanf("%d%d%d", &k, &n, &m);
        for (int i = 1; i <= n; i++)
            scanf("%d", &w[i]);
        for (int i = 1; i <= m; i++)
            scanf("%d", &d[i]);
        int cnt = 0;
        priority_queue<pli, vector<pli>, greater<pli>> q;
        for (int i = 1; i <= n; i++)
            q.push(make_pair(w[i], w[i]));
        while (k--) {
            pli x = q.top();
            q.pop();
            a[++cnt] = x.first;
            q.push(make_pair(x.first + x.second, x.second));
        }
        while (!q.empty())
            q.pop();
        for (int i = 1; i <= m; i++)
            q.push(make_pair(d[i], d[i]));
        k = cnt;
        cnt = 0;
        while (k--) {
            pli x = q.top();
            q.pop();
            b[++cnt] = x.first;
            q.push(make_pair(x.first + x.second, x.second));
        }
        LL ans = 0;
        for (int i = 1; i <= cnt; i++)
            ans = max(ans, a[i] + b[cnt + 1 - i]);
        printf("Case #%d: ", ++g);
        printf("%I64d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值