TOJ-1315 Software Company

A software developing company has been assigned two programming projects. As both projects are within the same contract, both must be handed in at the same time. It does not help if one is finished earlier.

This company has n employees to do the jobs. To manage the two projects more easily, each is divided into m independent subprojects. Only one employee can work on a single subproject at one time, but it is possible for two employees to work on different subprojects of the same project simultaneously.

Our goal is to finish the projects as soon as possible.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 11), the number of test cases, followed by the input data for each test case. The first line of each test case contains two integers n (1 ≤ n ≤ 100), and m (1 ≤ m ≤ 100). The input for this test case will be followed by n lines. Each line contains two integers which specify how much time in seconds it will take for the specified employee to complete one subproject of each project. So if the line contains x and y, it means that it takes the employee x seconds to complete a subproject from the first project, and y seconds to complete a subproject from the second project.

Output

There should be one line per test case containing the minimum amount of time in seconds after which both projects can be completed.

Sample Input

1
3 20
1 1
2 4
1 6

Sample Output

18



Source: Tehran 2004 Iran Nationwide

两个项目a和b,均分为m个子项目,由n个程序员合作完成。每个程序员每次完成一个子项目。求所需最短时间。

开始看这道题真的是一脸茫然,第一感觉是肯定是dp,但是怎么dp呢。只能说我对算法的掌握实在是太浅薄了。看了很多大神的

分析才知道有种方法叫二分答案。对这道题来说,所求答案是时间,我们就先求一个必然能完成两个项目的时间上限,由1到上限

二分,看二分值是否能让这两个项目完成。

判断能否在一定时间内完成两个项目就是dp了。dp[i][j]表示前i个人完成j个a项目的子项目最多能完成几个b项目的子项目,

最后若dp[n][m]>=m,就可以完成。

以下代码中,我将数组大小开到1000,因为TOJ中100会报错Runtime Error SIGSEGV

用如下二维数组的dp会超时,我做了一下优化,两部分的代码都贴出了。

#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
int dp[1100][1100];
int dp1[1100];
int a[1100];//做一个a子程序用时 
int b[1100];//做一个b子程序用时 
bool judge(int time,int n,int m){//在time之内能否完成 
    int i,j,k,r,s,mj;
    memset(dp,0,sizeof(dp));
    mj = 0;
    for(i=1;i<=n;i++){
        r = time/a[i];//time内第i人最多做的a数 
        for(k=0;k<=r;k++){
            s = (time-(k*a[i]))/b[i];//time内第i人最做k个a还能做的b数
            for(j=0;j<=mj;j++){
                int h = dp[i-1][j]+s;
                if(h>dp[i][j+k]) dp[i][j+k] = h;
            }
        }
        mj += r;
    }
    /*cout<<"time: "<<time<<endl;
    for(i=1;i<=n;i++){
        for(k=0;k<=m;k++){
            cout<<dp[i][k]<<" ";
        }
        cout<<endl;
    }*/
    if(dp[n][m]>=m){
        //cout<<"true "<<dp[n][m]<<endl;
        //cout<<endl;
        return true;
    }    
    else {
        //cout<<"false"<<endl;
        return false;
    }    
}
bool judge1(int time,int n,int m){   
        for(int i=0;i<1010;i++){  
            dp1[i]=-1;  
        }  
        dp1[0]=0;  
        for(int i=1;i<=n;i++){
               for(int j=m;j>=0;j--){
                    if(dp1[j] != -1){
                        for(int k=m;k>=0;k--){ 
                            if(a[i]*k <= time && j+k <= m)  
                                 dp1[j+k]=max(dp1[j+k],dp1[j]+(time-a[i]*k)/b[i]);  
                         }  
                     }  
               }
        }  
      return dp1[m] >= m;  
    }
int main(){
    int t,m,n,i,j,ma,mb,time,s,e,ans;
    cin>>t;
    while(t--){
        cin>>n>>m;
        ma = mb =0;
        for(i=1;i<=n;i++){
            cin>>a[i]>>b[i];
            if(a[i]>ma)
                ma = a[i];
            if(b[i]>mb)
                mb = b[i];
        }
        time = (ma+mb)*m;//最大所需时间
        s = 1; e = time;
        while(s<e){
            time = (s+e)/2;
            //cout<<s<<" "<<e<<" "<<time<<endl;
            if(judge1(time,n,m)){
                e = time;
                ans = time;
            }
            else{
                s = time + 1;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/shenchuguimo/p/6341323.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值