zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    [C++][编程风格]C++命名规则
    [Android]搜索关键字飞入飞出效果 (转)
    android,性能优化,内存优化管理,高级缓存 (转)
    Java内存泄露原因详解
    Java之线程(2) 调时器
    Java之线程(1) 传统线程机制的回顾
    Hibernate(1) 阻抗不匹配
    No4.传统线程同步通信技术
    Android内存溢出
    MAT Memory Analyzer Tool 插件装配(图解)(转)
  • 原文地址:https://www.cnblogs.com/Woo95/p/5255178.html
Copyright © 2011-2022 走看看