ZOJ 3331-Process the Tasks (DP)

Description

There are two machines A and B. There are n tasks, namely task 1, task 2, ..., task n. You must assign each task to one machine to process it. There are some facts you must know and comply with:

  • You must assign each task to only one machine to process.
  • At any time, a machine can process at most one task.
  • Task i (0 < i < n) can be processed if and only if each task j (0 < j < i) has been processed or processing.
  • If a task is processing on one machine, it cannot be interrupted.

You want to do finish all the tasks as soon as possible.

 

Input

 

There are multiple test cases. The first line of the input is an integer T (0 < T < 1000) indicating the number of test cases. Then T test cases follow. Each test case starts with an integer n (0 < n < 100). The ith line of the nextn lines contains two integers tAtB (0 < tAtB < 100), giving the time to process the ith task by machine A and machine B.

 

Output

 

For each test case, output the earliest time when all the tasks have been processed.

 

题意:

有两个机器,A和B,现在有N个任务,每个任务用A机器做需要ai时间,用B机器做需要bi时间。  a,b,n<100

每个任务只有在其之前的任务在做或者已经做完才可以开始做,每个机器只能同时做一个任务。

 

刚开始写了个n^3 dpTLE了,发现有1000组数据,然后思考n^2做法。

我们用dp[k][j]表示状态k,j时的总时间,k为0,1 代表是A的时间长还是B的时间长,j代表长的时间和短的时间的差值,由于差值小于等于a,b,所以复杂度n^2。

考虑任务是在A或B机器上完成转移即可,具体代码如下。

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int dp[105][2][105];
int a[2][105],n;
void up(int i,int k,int j,int v)
{
    dp[i][k][j]=min(dp[i][k][j],v);
}
int main()
{
    int T,i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++) scanf("%d%d",&a[0][i],&a[1][i]);
        memset(dp,1,sizeof(dp));
        dp[0][0][0]=0;
        dp[0][1][0]=0;
        for(i=1;i<=n;i++)
        {
            for(k=0;k<2;k++)
            {
                for(j=0;j<=100;j++)
                {
                    if(dp[i-1][k][j]>10000) continue;
                    up(i,k,a[k][i],dp[i-1][k][j]+a[k][i]);
                    if(a[k^1][i]>j)
                        up(i,1^k,a[k^1][i]-j,dp[i-1][k][j]-j+a[k^1][i]);
                    else up(i,k,j-a[1^k][i],dp[i-1][k][j]);
                }
            }
        }
        int ans=100000;
        for(k=0;k<2;k++)
            for(j=0;j<=100;j++) if(dp[n][k][j]) ans=min(ans,dp[n][k][j]);
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值