A Task Process
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1340 Accepted Submission(s): 663
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).
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
题目大意:有n个工人,来做A,B两种任务,其中A任务总共有x件,B任务总共有y件,给定每个工人干一件A任务和B任务所花费的时间。求这些人同时开始干这些任务,最少多长时间能把A、B两种任务都完成。
主题思路:分析本题可以发现,每个人分配的时间越长则每个人能做的任务也越多(可能相等,但是做的工作一定越多),也就是每个人分配的时间同最终完成任务的数量成正比例关系,即单调,所以这里可以二分时间。二分确定给每个人分配的时间。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dp[55][210];
int a[55], b[55];
int n, x, y;
bool DP(int T)
{
int i, j, k, xmin, iy;
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
for(i = 1; i <= n; i++)
{
for(j = 0; j <= x; j++) //枚举前i-1个人完成的a任务数
{
if(dp[i - 1][j] != -1)//前i-1个人完成能j个a任务,继续求解加上第i个人花费T时间的结果
{
xmin = min(T / a[i], x - j);
for(k = 0; k <= xmin; k++)//枚举第i个人完成a任务的数量来求完成b任务的数量
{
int iy = min((T - a[i] * k) / b[i], y - dp[i - 1][j]);
dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] + iy);
}
}
}
if(dp[i][x] >= y) return true; //如果前i个人生产了x个a后能够生产的b的数量大于y,则T时间里可以完成两件任务
}
if(dp[n][x] >= y)
return true;
else
return false;
}
int main()
{
int t, ans, i, j, l, r, mid;
scanf("%d", &t);
for(i = 1; i <= t; i++)
{
ans = 0;
scanf("%d%d%d", &n, &x, &y);
for(j = 1; j <= n; j++)
{
scanf("%d%d", &a[j], &b[j]);
}
l = 0;
r = a[1] * x + b[1] * y;//这里r可以这么设,因为不可能一个人完成所有任务会比大家一块完成快
while(l <= r)
{
mid = (l + r) >> 1;
if(DP(mid))
{
ans = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
printf("Case %d: %d\n", i, ans);
}
return 0;
}