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;
}
时间: 2024-10-24 06:43:04

ZOJ 3331-Process the Tasks (DP)的相关文章

ZOJ 1743 Concert Hall Scheduling(DP)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=743 题意:有两个音乐厅出租.给出n个租客,每个租客有个租的时间段[L,R],以及租费.任意时候音乐厅只能租给最多一个租客.问如何选择租给哪些租客使得赚的钱最多? 思路:f[i][j]表示第一个音乐厅到时刻i.第二个到时刻j,可以获得的最大值. struct node { int x,y,w; int operator<(const node &a) const

ZOJ 1642 Match for Bonus(dp)

Match for Bonus Time Limit: 2 Seconds      Memory Limit: 65536 KB Roy played a game with his roommates the other day. His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common char

zoj 3706 Break Standard Weight(dp)

Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 KB The balance was the first mass measuring instrument invented. In its traditional form, it consists of a pivoted horizontal lever of equal length ar

ZOJ 1642 Match for Bonus (DP)

题目链接 题意 : 给你两个字符串,两个字符串都有共同的字母,给你每个字母的值,规则是,找出两个字符串中的共同的一个字母,然后这个字母的值就可以加到自己的分数上,但是这步操作之后,这两个字母及其之前的字母都要删除掉,问你能够得到的最大的值是什么. 思路:最长公共子序列,加了一个权值. 1 #include <iostream> 2 #include <stdio.h> 3 #include <string> 4 #include <string.h> 5 6

ZOJ 3331 Process the Tasks

双塔DP. #include<cstdio> #include<cstring> #include<queue> #include<string> #include<algorithm> #include<map> #include<iostream> using namespace std; const int maxn=100+50; int T,n; int dp[maxn][2*maxn]; int a[maxn]

ZOJ 3631 Watashi&#39;s BG(dp+dfs)

题意:一共要吃n顿饭 公款m元 如果公款大于等于饭局所需费用 就全用公款 如果小于就自费 求最后能用的公款为多少 思路: dfs(i - 1, val + dp[i]); dfs(i - 1, val); #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; int dp[50]; int n, m; int a

ZOJ 2724 Windows 消息队列 (优先队列)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text cha

ZOJ 3644 Kitty&#39;s Game(DP)

Description Kitty is a little cat. She is crazy about a game recently. There are n scenes in the game(mark from 1 to n). Each scene has a number pi. Kitty's score will become least_common_multiple(x,pi) when Kitty enter the ith scene. x is the score

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom (DP)

Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we ca