2016 CCPC-Final-Wash(優先隊列+貪心)
阿新 • • 發佈:2019-04-26
contain output represent 還要 finish text pop limit rri
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!
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.
?1≤T≤100.
?1≤L≤106.
?1≤N,M≤105.
?1≤Wi,Di≤109.
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 ithAt 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.
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
?1≤T≤100.
?1≤L≤106.
?1≤N,M≤105.
?1≤Wi,Di≤109.
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 }
2016 CCPC-Final-Wash(優先隊列+貪心)