您的位置:首页 > 其它

Facebook Hacker Cup 2016 Round 1 Laundro, Matt

2016-10-23 22:26 323 查看


Facebook Hacker Cup 2016 Round 1

注意

Laundro, Matt20 points

Download
Input

Choose Output

No output file selected

Submit

Matt Laundro 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 Mdryers. The ith washing machine takes Wi minutes to wash one load of laundry, and each dryer takes D 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, Matt 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:

A non-negative amount of time after Matt arrives at the laundromat, Matt places the load in an unoccupied washing machine i
Wi minutes later, he removes the load from the washing machine, placing it in a temporary holding basket (which has unlimited space)
A non-negative amount of time later, he places the load in an unoccupied dryer
D minutes later, he removes the load from the dryer

Matt can instantaneously add laundry to or remove laundry from a machine. Help Matt minimize the amount of time (in minutes after he arrives at the laundromat) after which he can be done drying all L loads
of laundry!

Input

Input begins with an integer T, the number of times Matt goes to the laundromat. For each trip to the laundromat, there is first a line containing the space-separated integers LNM,
and D in that order. After that is a line containing N space-separated integers, the ith of which is Wi.

Output

For the ith trip, print a line containing "Case #i: " followed by the minimum time it will take Matt to finish his laundry.

Constraints

1 ≤ T ≤ 50 

1 ≤ L ≤ 1,000,000 

1 ≤ N ≤ 100,000 

1 ≤ M ≤ 1,000,000,000 

1 ≤ D ≤ 1,000,000,000 

1 ≤ Wi ≤ 1,000,000,000 

Explanation of Sample

In the first case, Matt has just one load of laundry. He washes it for 1200 minutes, and dries it for 34 minutes. In the second case, Matt uses the 1-minute washer for both loads of laundry. The second load finishes
at the 2-minute mark, so it finishes drying at the 12-minute mark.

Example input · Download

Example output · Download

5
1 1 1 34
1200
2 3 2 10
100 10 1
3 3 3 3
1 2 3
4 2 2 7
5 8
999 1 999 6
3


Case #1: 1234
Case #2: 12
Case #3: 5
Case #4: 22
Case #5: 3003


题意:给L件衣服,n个洗衣机,m个甩干机。洗衣机一次洗一件衣服,用时wi分钟,甩干机甩干一件衣服

用时D分钟,洗好的衣服可以放到篮子里面(此时不计耗时),问洗完并甩干L件衣服的最少时间。

思路:这个题目开始时候我就想洗衣服,甩干衣服这些过程之间的规律与联系,比如同一洗衣机洗衣时间要求和,

不同的去最大值,来算洗衣时间;甩干机要在洗衣服的时候甩干比较省时间。但知道这些还是无法下手。

于是有了另一种方法,问最少时间,是从第一件衣服开始洗计时,最后一件衣服甩干停止计时。

那其实就是求出每件衣服在尽可能节约时间情况下的总用时,最大的那件衣服的用时就是答案了。

那接下来就是怎么求出每件衣服在节约时间情况下的总用时了。时间很大,模拟时间线的方法是不现实的。

可以记录每个洗衣机,每个甩干机被用了多久,从开始到现在用时最少的则是最早空闲下来可以使用的。

洗衣机n的数量有10万,线性查找很慢,既然是求最少的一个,可以想到优先队列。

洗衣机的优先队列存储(所用时间,这个洗衣机洗单件衣服的时间),初始时的总时间跟洗一件衣服时间一样,

开始时就是选洗得最快的。接下来才是洗得比较快并且空闲下来了的。

答案优先队列存储每件衣服整个过程的总用时。

#include <bits/stdc++.h>

const int N = 1e5 + 10;
typedef long long ll;
using namespace std;
int num
;
typedef pair<ll, int> pli;
void run()
{
int L, n, m, d;
scanf("%d%d%d%d", &L, &n, &m, &d);
for (int i = 0; i < n; i++)
scanf("%d", num + i);

priority_queue<pli, vector<pli>, greater<pli> >wash;
priority_queue<ll, vector<ll>, greater<ll> >ans;
for (int i = 0; i < n; i++)
wash.push(pli((ll)num[i], num[i]));
m = min(m, L);//甩干机很多,但其实如果超过了衣服的数量,多余的甩干机是没有用的,所以取最小值
for (int i = 0; i < m; i++)
ans.push(0);
while (L--)
{
pli tmp = wash.top();wash.pop();//读取的tmp.first是当前洗衣服所能耗费的最小时间
wash.push(pli(tmp.first + tmp.second, tmp.second));//加上洗衣时间,因为下一个要用到这个
ll tim = ans.top(); ans.pop();//洗衣机的衣服就要等当前这件衣服洗完
ans.push(max(tim, tmp.first) + d);//取最大因为,可能最早用完的甩干机可能还没工作完
} //此时即使洗完了,也得排队等着,所以总时间就是最大时间
ll out;<span style="white-space:pre"> </span> //加甩干时间
while(!ans.empty())
{
out = ans.top();ans.pop();
}
printf("%lld\n", out);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r" ,stdin);
#endif
freopen("out.txt","w",stdout);
// ios::sync_with_stdio(0);
int T, cas = 1;
scanf("%d", &T);
while (T--)
{
printf("Case #%d: ", cas++);
run();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: