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;
    }
  • 相关阅读:
    HTML技巧篇:如何让单行文本以及多行文本溢出时显示省略号(…)
    SpringMVC中响应json数据(异步传送)
    如何用Spring框架的<form:form>标签实现REST风格的增删改查操作
    如何使用REST请求风格
    Spring插件的安装与卸载---笔记
    元素 "context:component-scan" 的前缀 "context" 未绑定的解决方案
    简单的文件上传的下载(动态web项目)
    用简单的反射优化代码(动态web项目)
    json数据与Gson工具类的使用
    JSON简介
  • 原文地址:https://www.cnblogs.com/Woo95/p/5255178.html
Copyright © 2011-2022 走看看