ZOJ 3331 Process the Tasks(双塔DP)
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 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 双塔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)的更多相关文章
- ZOJ 3331 Process the Tasks 双塔Dp
用dp[i][j]表示当前安排好了前i个任务,且机器A和机器B完成当前分配到的所有任务的时间差为j(这里j可正可负,实现的时候需要加个offset)时,完成这些任务的最早时间.然后根据j的正负,分别考 ...
- ZOJ 3331 Process the Tasks
双塔DP. #include<cstdio> #include<cstring> #include<queue> #include<string> #i ...
- ZOJ 2059 The Twin Towers(双塔DP)
The Twin Towers Time Limit: 2 Seconds Memory Limit: 65536 KB Twin towers we see you standing ta ...
- ZOJ2401 Zipper 双塔式 DP(双塔DP)
第二次遇到双塔DP,再写一下. (flag是为了避免memset多次导致的时间浪费) #include<cstdio> #include<cstdlib> #include&l ...
- HDU 3578 Greedy Tino(双塔DP)
Greedy Tino Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ 2609 Ferry Loading(双塔DP)
Ferry Loading Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1807 Accepted: 509 Sp ...
- VIJOS P1037搭建双塔[DP]
描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔. Mr. F有N块水晶,每块 ...
- ZOJ 3494 (AC自动机+高精度数位DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...
- POJ 1015 Jury Compromise(双塔dp)
Jury Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33737 Accepted: 9109 ...
随机推荐
- 关于cnblogs的排版
cnblogs默认的样式实在让我无法接受.今天去看了看div+css的基本知识. 决定这周抄一个别人的排版,稍微改改. 不然,就这样式,实在没心情写博客. 写博客,这次准备要大干一场了.
- 求1,1,2,3,5,8,13 斐波那契数列第N个数的值
朋友问了个斐波那契算法.我给出了个递归算法 public static int Foo(int n) { ) { return n; } else { ) + Foo(n - ); } } 结果被打击 ...
- intellij中常用的快捷键
intellij快捷键
- unity prefab使用原则
prefab可无限apply: 如果把一个模块做成了prefab,这个prefab可能在同一个scene中添加多个,甚至添加到了多个scene中.设所有这些实例为instance(1),instanc ...
- C# ZPL II 命令打印标签
public class BarPrinter { public static byte[] ZPLPrinter(string p_title = "", string p_sp ...
- Java对象的浅克隆和深克隆
为什么需要克隆 在实际编程过程中,我们常常要遇到这种情况:有一个对象A,在某一时刻A中已经包含了一些有效值,此时可能会需要一个和A完全相同新对象B, 并且此后对B任何改动都不会影响到A中的值 ...
- Linux定时备份数据到百度云盘
导读:如今的百度云盘免费容量都是2T了,即使把电脑上所有的东东都放上去,也还有大把的剩余空间.对于站长来说,是完全可以充分利用这些硬盘空间的,现在我们就用百度云盘来备份Linux服务器上的数据. 一直 ...
- 有效提升大数据量写入excel的效率
在开发过程中经常会有需要将数据导出到 excel 的需求,当数据量很大,达到几万甚至几十万.几百万级别的时候,如何加快生成 excel 的速度呢?首先普及一下知识背景:Excel2003 及以下版本一 ...
- HDU 1075 What Are You Talking About (Trie)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- Servlet 容器对URI的处理
问题 请求到达server以后,server是怎样处理URI请求资源路径的,在与web.xml文件里的映射进行比对时的原则是什么. 方案 针对精确匹配.通配符匹配.后缀匹配三种模式改动web.xml文 ...