Tom’s Kitchen
Tom’s Kitchen 1 sec / 10 sec 256 MB
Tom’s Kitchen is a very popular restaurant. One of the reasons for its popularity is that every
single meal is prepared by at least K different chefs. Today, there are N meals to be prepared,
with meal i needing Ai hours of work.
There are M chefs which Tom can hire to prepare all the meals but the chef j will work at most
Bj hours. Additionally, even when he works less, he still wants to be paid for the full Bj hours.
A chef can work on several meals for different amounts of time, but any meal will be properly
prepared only if at least K chefs take part in preparing it and the total time they spend is
exactly Ai
. When a chef takes part in preparing a meal, he always works on it some positive
integer number of hours.
Tom needs help in choosing the optimal subset of chefs such that the sum of hours where the
chefs are getting paid without working is minimized.
Input. The first line contains the integers N, M, and K.
The second line contains N integers Ai and the third line M integers Bj .
Output. The only line should contain the number of hours the chefs spend not working but
still getting paid when Tom chooses the optimal subset to hire. If there is no way to prepare all
the N meals according to the rules described above, output “Impossible”.
Example. Input
1 2 2
5
3 4
Output
2
Here Tom needs two chefs to work on the meal, so he must hire both that are available. Then
it does not matter how they divide the work, as they end up working a total of 5 hours, but
getting paid for 3 + 4 = 7 hours, and thus getting paid for 2 extra hours.
Example. Input
1 1 2
5
5
Output
Impossible
Here Tom needs two chefs to work on the meal, but only one is available.
Example. Input
3 3 3
3 3 2
3 3 3
Output
Impossible
Here meal 3 can’t be prepared by three chefs, as each would have to work for at least an hour,
but the meal takes only 2 hours to prepare.
Grading. The test groups satisfy the following conditions:
- (9 points) 1 ≤ N, K ≤ 300, 1 ≤ M ≤ 2, 1 ≤ Ai, Bj ≤ 300.
- (22 points) 1 ≤ N, K ≤ 300, 1 ≤ M ≤ 15, 1 ≤ Ai, Bj ≤ 300.
- (20 points) 1 ≤ N, M, Ai, Bj ≤ 300, K = 1.
- (21 points) 1 ≤ N, M, K, Ai, Bj ≤ 40.
- (28 points) 1 ≤ N, M, K, Ai, Bj ≤ 300. 7
题目大意
有
n
n
n块肉,第
i
i
i块有
A
i
A_i
Ai斤,有
m
m
m个厨师,第
i
i
i个厨师最多工作
B
i
B_i
Bi个小时,而且没小时只能处理1斤肉,每一块肉至少需要
k
k
k个厨师参与处理,现在雇佣一些厨师使得他们的摸鱼时间(不工作时间)之和最少。
如果不行输出"Impossible";
题解
首先分析发现如果合法,那么答案应该是所有选择的厨师时间之和减去肉量之和。
接下来考虑合法。
由于如果有肉的质量小于k,那么无解,所以当所有的肉都大于k时,我们可以把每块肉切k斤下来,使得这k斤的处理厨师都不同,剩下的就可以随意分配了。我们把切下来的肉排成一个n*k的肉排:
很明显如果一个厨师的时间大于n,那么他最多也只可以在这个肉排里面工作n小时,否则可以工作完。
我们优先让厨师在这里工作,如果肉排被处理完了,而且总工作时间大于肉总量,那么就是合法的。
于是不难想出以下DP
d
p
[
i
]
dp[i]
dp[i]表示总工作为i的前提下,在猫排工作的最大时间,那么
d
p
[
i
+
b
]
=
m
a
x
(
d
p
[
i
+
b
]
,
d
p
[
i
]
+
m
i
n
(
b
,
n
)
)
;
dp[i+b]=max(dp[i+b],dp[i]+min(b,n));
dp[i+b]=max(dp[i+b],dp[i]+min(b,n));
所以从总肉量开始向上找到最小的符合
d
p
[
i
]
>
n
∗
k
dp[i]>n*k
dp[i]>n∗k的i即可。
下面是代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=305,MAXM=90000;
int n,m,k,ans,jl=0;
int dp[MAXN*MAXN];
int main()
{
freopen("kitchen.in","r",stdin);
freopen("kitchen.out","w",stdout);
scanf("%d %d %d",&n,&m,&k);
for(int i=1;i<=n;i++)
{
int a;
scanf("%d",&a);
if(a<k)
{
printf("Impossible\n");
return 0;
}
jl+=a;
}
memset(dp,-1,sizeof(dp));
dp[0]=0;
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d",&b);
a=min(n,b);
for(int j=MAXM-b;j>=0;j--)
{
if(dp[j]!=-1)
{
dp[j+b]=max(dp[j+b],dp[j]+a);
}
}
}
for(int i=jl;i<=MAXM;i++)
{
if(dp[i]>=n*k)
{
printf("%d\n",i-jl);
return 0;
}
}
printf("Impossible\n");
fclose(stdin);
fclose(stdout);
}