1. 程式人生 > >[貪心 優先佇列] 兩步驟完成多項工作總用時最短 HDU6000

[貪心 優先佇列] 兩步驟完成多項工作總用時最短 HDU6000

Wash

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 64000/64000 K (Java/Others)
Total Submission(s): 2492    Accepted Submission(s): 672


 

Problem Description

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


∙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

 

 

Source

2016 CCPC-Final

 

 

使總時間最短
洗衣完成較早者對應烘乾所需時間最小的L項中較遲者
時限卡得很緊

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int ml = 1e6 + 10;

ll A[ml];
struct node
{
	int id;
	ll time;
} a[ml], b[ml];
bool operator <(const node& a, const node& b)
{
	return a.time > b.time;
}
priority_queue<node> q1, q2;

int main()
{
	int T;
	scanf("%d", &T);
	for (int cas = 1; cas <= T; cas++)
	{
		while (!q1.empty())
			q1.pop();
		while (!q2.empty())
			q2.pop();
	
		int l, n, m;
		scanf("%d %d %d", &l, &n, &m);
		for (int i = 1; i <= n; i++)
		{
			a[i].id = i;
			scanf("%lld", &a[i].time);
			q1.push(a[i]);
		}
		for (int i = 1; i <= m; i++)
		{
			b[i].id = i;
			scanf("%lld", &b[i].time);
			q2.push(b[i]);
		}
		
		// 洗衣完成時間從小到大
		for (int i = 0; i < l; i++)
		{
			node t = q1.top();
			A[i] = t.time;
			q1.pop();
			t.time += a[t.id].time;
			q1.push(t);
		}
		
		ll ans = 0;
		// 烘乾完成時間與 A[i] 反著對應
		for (int i = l - 1; i >= 0; i--)
		{
			node t = q2.top();
			ans = max(ans, t.time+ A[i]);
			q2.pop();
			t.time += b[t.id].time;
			q2.push(t);
		}
		printf("Case #%d: %lld\n", cas, ans);
	}
	return 0;
}