HDU6000 Wash

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

这是一类经典的排队问题,常常使用优先队列进行排序。机器按所花时间从小到大排序(常常使用到运算符重载),优先使用前面的机器(使用完后出队),当一台机器使用完后其权值发生变化,其作为一台新机器重新入队排序。

此题有两类机器,分别需要一个优先队列储存,它们之间关联使用贪心策略:令最晚洗完的衣服用最快的烘干机器。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
long long t[1000001], ans;
int test;
long long l, n, m;
struct node
{
	long long x, y;                            //每个机器记录两个数值,x是初始用所需的时间,y是以后再用所需的时间,y是变化的
	friend bool operator <(node a, node b)     //重载运算符,从小到大排序
	{
		return a.y > b.y;
	}
}cloth;
priority_queue<node> q1, q2;
int main()
{
	scanf("%d", &test);
	for(int j=1;j<=test;j++)
	{
		ans = 0;
		while (!q1.empty()) q1.pop();         //清空队列!
		while (!q2.empty()) q2.pop();
		scanf("%d%d%d", &l, &n, &m);
		for (int i = 0; i < n; i++)           //初始化两种机器
		{
			scanf("%lld", &cloth.x);
			cloth.y = cloth.x;
			q1.push(cloth);
		}
		for (int i = 0; i < m; i++)
		{
			scanf("%lld", &cloth.x);
			cloth.y = cloth.x;
			q2.push(cloth);
		}
		for (int i = 0; i < l; i++)
		{
			cloth = q1.top();      
			q1.pop();
			t[i] = cloth.y;                   //t数组记录该衣服洗完的最快时间
			cloth.y += cloth.x;               //此台机器用完,时间增加,作为新机器重新入队列
			q1.push(cloth);
		}
		for (int i = l - 1; i >= 0; i--)
		{
			cloth = q2.top();
			q2.pop();
			ans = max(ans, t[i] + cloth.y);   
			cloth.y += cloth.x;               //烘干机器同理
			q2.push(cloth);
		}
		printf("Case #%d: %lld\n",j, ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值