ZOJ3331---Process the Tasks(dp)

 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 next n lines contains two integers tA, tB (0 < tA, tB < 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.

Sample Input

4
1
1 2
2
1 2
2 1
2
1 2
90 95
3
1 3
1 3
1 3

Sample Output

1
1
90
3

Hints

Test case 1: Let machine A process task 1.
Test case 2: Let machine A process task 1 and at the same time let machine B process task 2.
Test case 3: Let machine B process task 1 and at the same time let machine A process task 2.
Test case 4: Let machine A process all the tasks.

一类经典的双塔dp

dp[i][j]表示做完前i项任务,A和B时间差为j时,完成任务的最短时间

j会为负,递推时加上偏移量

j>=0, 说明A要忙,如果第i+1项任务由A来做的话,那么B会空闲到直到A开始做第i+1项任务,不然B无法做新的任务,此时为:

dp[i+1][ta[i+1]]=dp[i][j]+ta[i+1];

如果把第i+1项任务交给B来做

dp[i+1][j?tb[i+1]]=dp[i][j]+max(tb[i+1]?j,0);

j<0 情况类似

/*************************************************************************
    > File Name: ZOJ3331.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月14日 星期二 16时14分15秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 120;
int dp[N][2 * N];
int ta[N], tb[N];

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d%d", &ta[i], &tb[i]);
        }
        memset(dp, inf, sizeof(dp));
        dp[0][100] = 0;
        for (int i = 0; i < n; ++i)
        {
            for (int j = -100; j <= 100; ++j)
            {
                if (j >= 0)
                {
                    dp[i + 1][100 + ta[i + 1]] = min(dp[i + 1][100 + ta[i + 1]], dp[i][j + 100] + ta[i + 1]);
                    dp[i + 1][100 + j - tb[i + 1]] = min(dp[i + 1][100 + j - tb[i + 1]], dp[i][j + 100] + max(tb[i + 1] - j, 0));
                }
                else
                {
                    dp[i + 1][-tb[i + 1] + 100] = min(dp[i + 1][-tb[i + 1] + 100], dp[i][j + 100] + tb[i + 1]);
                    dp[i + 1][100 + j + ta[i + 1]] = min(dp[i + 1][100 + j + ta[i + 1]], dp[i][j + 100] + max(ta[i + 1] + j, 0));
                }
            }
        }
        int ans = inf;
        for (int i = -100; i <= 100; ++i)
        {
            ans = min(ans, dp[n][i + 100]);
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-11-07 01:27:46

ZOJ3331---Process the Tasks(dp)的相关文章

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]

Java Secure Socket Extension (JSSE) Reference Guide

Skip to Content Oracle Technology Network Software Downloads Documentation Search Java Secure Socket Extension (JSSE) Reference Guide This guide covers the following topics: Skip Navigation Links Introduction Features and Benefits JSSE Standard API S

Java多线程系列--“JUC线程池”04之 线程池原理(三)

本章介绍线程池的生命周期. 线程有5种状态:新建状态,就绪状态,运行状态,阻塞状态,死亡状态.线程池也有5种状态:然而,线程池不同于线程,线程池的5种状态是:Running, SHUTDOWN, STOP, TIDYING, TERMINATED. 线程池状态定义代码如下: /** * The main pool control state, ctl, is an atomic integer packing * two conceptual fields * workerCount, indi

Java并发基础(六) - 线程池

Java并发基础(六) - 线程池 1. 概述 这里讲一下Java并发编程的线程池的原理及其实现 2. 线程池的基本用法 2.1 线程池的处理流程图 该图来自<Java并发编程的艺术>: 从图中我们可以看出当一个新任务到线程池时,线程池的处理流程如下: 线程池首先判断线程池里面线程数是否达到核心线程数.如果不是则直接创建新线程作为核心线程来执行该任务(该线程作为核心线程不会由于任务的完成而销毁),否则进入下一流程. 判断阻塞队列是否已经满了.如果没满则将该任务放入阻塞队列中,等待核心线程处理,

Report processing of Microsoft Dynamic AX

Report processing of Microsoft Dynamic AX The implementation of a general electronic report usually has four classes. Contract: Comment: Contract class is data contract class for SSRS report . Intent: Gets or sets the value of the data contract param

Celery - Best Practices

If you've worked with Django at some point you probably had the need for some background processing of long running tasks. Chances are you've used some sort of task queue, and Celery is currently the most popular project for this sort of thing in the

在top命令下kill和renice进程

For common process management tasks, top is so great because it gives an overview of the most active processes currently running (hence the name top). This enables you to easily find processes that might need attention. From top, you can also perform

QtGui.QProgressBar

A progress bar is a widget that is used when we process lengthy tasks. It is animated so that the user knows that the task is progressing. The QtGui.QProgressBar widget provides a horizontal or vertical progress bar in PyQt4 toolkit. The programmer c

J.U.C--线程池ThreadPoolExecutor

这一篇博文主要讲解关于Java的线程池相关的内容,主要包括: (1) Executor接口以及其子接口 (2)Executor的生命周期 (3)Executors (4)任务拒绝策略 (5)线程池 ThreadPoolExecutor实现原理 1. Executor接口以及其子接口 首先来看一下线程池相关类与接口的体系结构图: 上面系很清晰显示了线程池中的常用类和接口之间关系,下面首先看看最基础的Executor接口: public interface Executor { /** * Exec