二分+DP HDU 3433 A Task Process

时间:2022-09-25 03:19:17

HDU 3433 A Task Process

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1368    Accepted Submission(s):
684

Problem Description
There are two kinds of tasks, namely A and B. There are
N workers and the i-th worker would like to finish one task A in ai minutes, one
task B in bi minutes. Now you have X task A and Y task B, you want to assign
each worker some tasks and finish all the tasks as soon as possible. You should
note that the workers are working simultaneously.
 
Input
In the first line there is an integer T(T<=50),
indicates the number of test cases.

In each case, the first line contains
three integers N(1<=N<=50), X,Y(1<=X,Y<=200). Then there are N
lines, each line contain two integers ai, bi (1<=ai, bi <=1000).

 
Output
For each test case, output “Case d: “ at first line
where d is the case number counted from one, then output the shortest time to
finish all the tasks.
 
Sample Input
3
2 2 2
1 10
10 1
2 2 2
1 1
10 10
3 3 3
2 7
5 5
7 2
Sample Output
Case 1: 2
Case 2: 4
Case 3: 6
 /*
二分+DP。
二分时间t,dp[i][j]表示在时间t内前i个人完成j件A任务所能完成的B任务的最大数量。如果dp[i][x]>=y
就是可以的。然后不断迭代得到ans。
*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 70
int T,n,x,y,a[N],b[N];
void input(int &r)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
scanf("%d%d%d",&n,&x,&y);
for(int i=;i<=n;++i)
{
scanf("%d%d",&a[i],&b[i]);
r=max(r,max(a[i],b[i]));
}
}
bool check(int tim)
{
int f[]={};
memset(f,-,sizeof(f));
f[]=;
for(int i=;i<=x;++i)
if(tim>=a[]*i)
f[i]=(tim-a[]*i)/b[];
if(f[x]>=y) return true;
for(int i=;i<=n;++i)
{
for(int k=x;k>=;--k)
for(int j=k;j>=;--j)/*for(int j=0;j<=k;--j),结果更新f[i][k]的时候用的是他本身,如果改为for(int j=0;j<k;--j),又不能用f[i-1][k]来更新f[i][k],所以就改为了倒序*/
if(tim>=(k-j)*a[i]&&f[j]!=-)
f[k]=max(f[k],f[j]+(tim-a[i]*(k-j))/b[i]);
if(f[x]>=y) return true;
}
return false;
}
int find_ans(int l,int r)
{
int mid;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid))
{
r=mid-;
}
else l=mid+;
}
return l;
}
int main()
{
scanf("%d",&T);
int topt=;
while(T--)
{
++topt;
int l=,r=;
input(r);
r=r**max(x,y);
printf("Case %d: %d\n",topt,find_ans(l,r));
}
return ;
}