A Task Process (Hangzhou Electric OJ 3433)

A Task Process

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


 

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

 

 

 

 

 

Shrinking takes time until the critical value (using binary search)

0 dp not initialize an array, as dp [i] [j] in front did not reach this state, i.e., before the individual i j is no way to complete the task within a time period A, so that a [i + 1] [ j + k] and do not use this transition state, because the state does not exist 

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int t,n,x,y;   //x个A 任务, Y个 B任务 
int a[1002],b[1002];
bool solve(int time) {     //尝试time个时间是否可以完成所有任务 
	//dp[n][X]= dp[n-1][x-i]+(time-i*a[n])/b[n]  //n表示前n个人,完成X个A任务
	int dp[52][202];
	memset(dp,-1,sizeof(dp));
	for(int i=0;i*a[1]<=time;i++){
		dp[1][i]=(time-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]<=time&&k<=j; k++) {
				if(dp[i-1][j-k]>=0)dp[i][j]=max(dp[i][j],dp[i-1][j-k]+(time-k*a[i])/b[i]);
			}
		}
	}
	if(dp[n][x]>=y)return true;
	return false;
}
int main() {
	cin>>t;
	for(int k=1;k<=t;k++){
		cin>>n>>x>>y;
		for(int i=1; i<=n; i++) {
			cin>>a[i]>>b[i];
		}
		int l=1,r=a[1]*x+b[1]*y,time=r;
		while(l<=r) {  
			int m=(l+r)/2;
			if(solve(m)) { 
				time=m;
				r=m-1;
			} else {
				l=m+1;
			}
		}
		cout<<"Case "<<k<<": "<<time<<endl;
	}


	return 0;
}

 

Published 42 original articles · won praise 16 · views 3401

Guess you like

Origin blog.csdn.net/qq_41542638/article/details/102685604