hdu 3433 A Task Process 二分+dp

 

A Task Process

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

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
/*
hdu 3433 A Task Process 二分+dp(卒)

dp方面毕竟若,着实没有想出来状态转移方程
主要是数据特别小,可以考虑二分答案然后通过判断来解决
如果知道了能够使用的时间limi.假设dp[i][j]表示前i个人完成j个A任务时最多能完成多少
个B任务
转移方程:
dp[i][j] = (dp[i-1][j-k] + (limi-k*a[i])*b[i],dp[i][j])

hhh-2016-04-10 21:02:38
*/
#include <iostream>
#include <cstdio>
#include <cstring>
typedef long long ll;
using namespace std;
const int mod = 1e9+7;
const int maxn = 205;
int a[maxn],b[maxn];
int x,y,n;
int dp[maxn][maxn];
bool cal(int limi)
{
    //dp[i][j] 前i个人完成j个A任务的情况下,最多完成多少个B
    memset(dp,-1,sizeof(dp));
    for(int i =0; i <= x && i*a[1] <= limi; i++)
    {
        dp[1][i] = (limi-i*a[1])/b[1];
    }

    for(int i = 2; i <= n; i++)
    {
        for(int j = 0; j <= x; j++)
        {
            for(int k = 0; k*a[i] <= limi && k <= j; k++)
            {
                if(dp[i-1][j-k] >= 0)
                    dp[i][j] = max(dp[i][j],dp[i-1][j-k]+(limi-k*a[i])/b[i]);
                //如果不是同一个工人,那么工作进而同时进行
            }
        }
    }
    return dp[n][x] >= y;
}

int main()
{
    int T;
    int cas = 1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&x,&y);
        int ma = 0;
        for(int i =1 ; i <= n; i++)
        {
            scanf("%d%d",&a[i],&b[i]);
            ma = max(ma,a[i]);
        }
        int l = 0,r = ma*x;
        int ans = 0;
        while(l <= r)
        {
            int mid = (l+r)>>1;

            if(cal(mid))
            {
                ans = mid;
                r = mid-1;
            }
            else
                l = mid + 1;
        }
        printf("Case %d: %d\n",cas++,ans);
    }
    return 0;
}

  



 

posted @ 2016-04-16 22:08  Przz  阅读(237)  评论(0编辑  收藏  举报