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
", cnt++, ans);
50     }
51     return 0;
52 }
 
原文地址:https://www.cnblogs.com/graytido/p/10770895.html