Process the Tasks


Time Limit: 1 Second      Memory Limit: 32768 KB


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 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.

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 双塔DP dp[i][j] 表示执行第i个任务,机器A和机器B的时间差 双塔DP 顾名思义可以把A机器和B机器看作是两座塔,如果把当前任务放在哪个塔上,那么哪个塔的高度就会增加 每次放任务,都会有两个选择,要么放在A塔,要么放在B塔。如果放在高的塔上,根据题意在同一个机器上必要上一个任务完成 时才能进行下一个任务,所以等高的塔完成上一个任务是,A,B都是空闲的了,这个是在高的塔上放任务,时间差就是这个任务的时间 如果放在低的塔上,那么在之前的差值上加上这个任务的时间
#include <iostream>
#include <string.h>
#include <stdlib.h> #include <math.h>
#include <stdio.h> using namespace std;
#define MAX 10000000
int dp[105][205];
int n;
int ta[105];
int tb[105];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&ta[i],&tb[i]);
for(int i=0;i<=n;i++)
for(int j=0;j<=200;j++)
dp[i][j]=MAX;
dp[0][0+100]=0;
for(int i=1;i<=n;i++)
{
for(int j=-99;j<=99;j++)
{
if(dp[i-1][j+100]==MAX)
continue;
if(j<0)
{
dp[i][-tb[i]+100]=min(dp[i][-tb[i]+100],dp[i-1][j+100]+tb[i]);
dp[i][j+ta[i]+100]=min(dp[i][j+ta[i]+100],dp[i-1][j+100]+max(0,j+ta[i]));
}
else
{
dp[i][ta[i]+100]=min(dp[i][ta[i]+100],dp[i-1][j+100]+ta[i]);
dp[i][j-tb[i]+100]=min(dp[i][j-tb[i]+100],dp[i-1][j+100]+max(0,tb[i]-j));
}
}
}
int ans=MAX;
for(int i=-99;i<=99;i++)
ans=min(ans,dp[n][i+100]);
printf("%d\n",ans);
}
return 0;
}

ZOJ 3331 Process the Tasks(双塔DP)的更多相关文章

  1. ZOJ 3331 Process the Tasks 双塔Dp

    用dp[i][j]表示当前安排好了前i个任务,且机器A和机器B完成当前分配到的所有任务的时间差为j(这里j可正可负,实现的时候需要加个offset)时,完成这些任务的最早时间.然后根据j的正负,分别考 ...

  2. ZOJ 3331 Process the Tasks

    双塔DP. #include<cstdio> #include<cstring> #include<queue> #include<string> #i ...

  3. ZOJ 2059 The Twin Towers(双塔DP)

    The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing ta ...

  4. ZOJ2401 Zipper 双塔式 DP(双塔DP)

    第二次遇到双塔DP,再写一下. (flag是为了避免memset多次导致的时间浪费) #include<cstdio> #include<cstdlib> #include&l ...

  5. HDU 3578 Greedy Tino(双塔DP)

    Greedy Tino Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. POJ 2609 Ferry Loading(双塔DP)

    Ferry Loading Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1807   Accepted: 509   Sp ...

  7. VIJOS P1037搭建双塔[DP]

    描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔. Mr. F有N块水晶,每块 ...

  8. ZOJ 3494 (AC自动机+高精度数位DP)

    题目链接:  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...

  9. POJ 1015 Jury Compromise(双塔dp)

    Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33737   Accepted: 9109 ...

随机推荐

  1. mongodb - 查看正在执行的操作

    查看正在执行的操作 db.currentOp() 查看系统执行的操作 db.currentOp(True) kill正在执行的操作 db.killOp(<operation id>) 示例 ...

  2. 深度学习之Matlab 转C++在iOS上測试CNN手型识别

    1 前言 在上一篇Blog.我介绍了在iOS上执行CNN的一些方法. 可是,一般来说.我们须要一个性能强劲的机器来跑CNN,我们仅仅只是须要将得到的结果用于移动端. 之前在Matlab使用UFLDL的 ...

  3. html5-canvas绘图操作方法

    <script>function draw(){    var c=document.getElementById("mycanvas");    c.width=50 ...

  4. Mac 常用的手势

    可以在触屏版-更多手势查看 技巧:前期慢慢滑动练习.一定要慢慢滑动,这样可以清楚的看出有没有产生效果,尤其注意大拇指滑动的感觉. 回到桌面:四指向外 打开Lanuchpad:四指向内 查看所有任务:三 ...

  5. Centos系统安装JDK详细图文教程

    1.查询系统默认JDK Centos系统默认会安装OpenJDK,一般建议是安装sun公司的JDK.我们首先检查系统是否安装有jdk并且是OpenJDK版本的,若是,则将它卸载掉并安装上sun公司的j ...

  6. Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案

    Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案 1. 业务场景 android+webview h5 css背景图性能提升1 2. ...

  7. 浅谈 Objective-C 下对象的初始化

    转自:http://www.oschina.net/question/54100_32468 众所周知,Objective-C是一门面向对象的语言,一般情况下,我们在Objective-C中定义一个类 ...

  8. God web

    http://my.csdn.net/yerenyuan_pku http://evilcos.me/?p=336 https://www.zhihu.com/question/21606800 st ...

  9. 3.Queues(队列)

    一.概述 C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构,与stack刚好相反. 二.常用API back() 返回最后一个元素 empty() 如果队列空则返回真 fro ...

  10. update-alternatives

    转自: http://lingavin.com/blog/2013/03/07/alternatives-gcc/ Ubuntu-gcc版本切换 MAR 7TH, 2013 随着ubuntu版本的更新 ...