Gym 101206 B Wash

Gym 101206 B Wash

題意

LL 件衣服, nn 臺洗衣機,每臺洗衣機洗一件衣服需要 w[i]w[i] 的時間

mm 臺烘乾機,每臺烘乾機烘乾一件衣服需要 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章