2016 CCPC-Final-Wash(优先队列+贪心)

本文探讨了一种洗衣房中洗衣机和烘干机的最优调度策略,旨在通过贪心算法和优先队列实现衣物清洗和烘干的最短总时间。具体策略为,首先选择最快的洗衣机进行衣物清洗,然后将衣物放置于最快完成工作的烘干机中,以此循环直至所有衣物完成清洗和烘干,最终目标是最小化最后一堆衣物完成烘干所需的时间。

                           Wash

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


1T100.
1L106.
1N,M105.
1Wi,Di109.
 

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个洗衣机洗一堆衣服要的时间n[i]和m个烘干机烘干一堆衣服要的时间m[i],
衣服洗了可以先放着不烘干,想要得到最快洗完并烘干所有衣服所要的时间。
解题思路:先贪心最快洗完衣服的时间,要使得总时间最小,则最晚洗完的衣服应该用最快的烘干机烘干,所以先把洗衣时间排进优先队列,
洗过衣服的洗衣机再洗一遍可能比没洗过的洗衣机还要快(一个洗衣机多次使用的情况),取出队列队首之后再把队首加上已使用的时间再加入队列,
洗烘干衣服同理,烘干所需的最长时间取决于最后烘干完的衣服,取衣服烘干的过程中取Max
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 typedef unsigned long long ull;
 5 #define INF 0x3f3f3f3f
 6 const ll MAXN = 1e6 + 7;
 7 const ll MOD = 1e9 + 7;
 8 const double pi = acos(-1);
 9 ll w, d;
10 ll cah1[MAXN], cah2[MAXN];
11 typedef pair<ll, ll> p;
12 int main()
13 {
14     int t, cnt = 1;
15     scanf("%d", &t);
16     while (t--)
17     {
18         priority_queue<p, vector<p>, greater<p> > q1;
19         priority_queue<p, vector<p>, greater<p> > q2;
20         ll l, n, m;
21         p t;
22         scanf("%lld%lld%lld", &l, &n, &m);
23         for (int i = 0; i < n; i++)
24         {
25             scanf("%lld", &w);
26             q1.push(p(w, w));
27             //洗衣服的时间 和第几台
28         }
29         for (int i = 0; i < m; i++)
30         {
31             scanf("%lld", &d);
32             q2.push(p(d, d));
33         }
34         for (int i = 0; i < l; i++)
35         {
36             t = q1.top();
37             q1.pop();
38             cah1[i] = t.first;                        //cah存时间
39             q1.push(p(t.first + t.second, t.second)); //若是时间小继续使用
40         }
41         ll ans = 0;
42         for (int i = l - 1; i >= 0; i--)
43         {
44             t = q2.top();
45             q2.pop();
46             ans = max(t.first + cah1[i], ans);
47             q2.push(p(t.first+t.second,t.second));
48         }
49         printf("Case #%d: %lld\n", cnt++, ans);
50     }
51     return 0;
52 }

 

 

转载于:https://www.cnblogs.com/graytido/p/10770895.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值