用dp[i][j]表示当前安排好了前i个任务,且机器A和机器B完成当前分配到的所有任务的时间差为j
(这里j可正可负,实现的时候需要加个offset)时,完成这些任务的最早时间。
然后根据j的正负,分别考虑任务i+1的两种分配方法。比如j大于0,A比B后空闲,
这个时候如果再把任务分配给A的话,B空闲知道A开始处理i+1的这段时间,B是不能安排任务的,
也就是说可以看成是非空闲的状态,于是下一个状态的A和B时间差就是i+1任务的长度。 就根据这个思路分几个情况进行处理,可以知道j这维不会超过100(就是上面这个原因),整个程序是100^2的复杂度。

  

Source Code:

//用dp[i][dt]表示当前安排好了前i个任务,且机器A和机器B完成当前分配到的所有任务的时间差为dt

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const int INF = 0x3f3f3f3f ;
const int offset = ; int dp[][], dt;
int ta[], tb[];
int n; int main () {
std::ios::sync_with_stdio (false);
int i, j, t, k, u, v, numCase = ;
cin >> t;
while (t--) {
cin >> n;
for (i = ; i <= n; ++i) {
cin >> ta[i] >> tb[i];
}
memset (dp, 0x3f, sizeof(dp));
dp[][ + offset] = ; for (i = ; i <= n; ++i) {
for (dt = -; dt <= ; ++dt) {
if (dp[i - ][dt + offset] == INF) continue; if (dt < ) { // B 机器人的工作时间更长
checkmin (dp[i][-tb[i] + offset], dp[i - ][dt + offset] + tb[i]); //放在B上面(继续由机器人B加工
checkmin (dp[i][dt + ta[i] + offset], dp[i - ][dt + offset] + Max(, ta[i] + dt));//放在A上面,继续让机器人A加工
} else { // A 机器人的工作时间更长
checkmin (dp[i][ta[i] + offset], dp[i - ][dt + offset] + ta[i]); //放在A上面,继续让机器人A加工
checkmin (dp[i][dt - tb[i] + offset], dp[i - ][dt + offset] + Max(, tb[i] - dt));//放在B上面,继续由机器人B加工
}
}
} int res = INF;
for(dt = -; dt <= ; ++dt){
checkmin (res, dp[n][dt + offset]);
}
cout << res << endl;
}
return ;
}

ZOJ 3331 Process the Tasks 双塔Dp的更多相关文章

  1. ZOJ 3331 Process the Tasks(双塔DP)

    Process the Tasks Time Limit: 1 Second      Memory Limit: 32768 KB There are two machines A and B. T ...

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

    配置python环境变量我的电脑->右键->属性->高级系统设置->环境变量->(1)用户变量->新建 Path=C:\Python27(2)系统变量->编辑 ...

  2. [转]IOS 中文排序

    转自:http://www.cnblogs.com/syxchina/archive/2012/10/11/2720257.html 1 原因 Ios默认使用utf-8格式编码,所以中文在IOS中默认 ...

  3. struts2 taglib struts标签学习整理中

    项目中经常会用到struts标签,这样可以减少代码量. select用法: <s:select list="#request.sysTypes" name="rul ...

  4. 灵动标签内sql语句调用

    本节来介绍帝国cms中,灵动标签中如何写数据库调用我们所要的信息.方便一些没有学习过数据库的朋友 转载请注明出处:谢寒的博客 灵动标签默认的语法 [e:loop={栏目ID/专题ID,显示条数,操作类 ...

  5. Oracle 表空间操作

    -- 查询已有表空间 SELECT TABLE_SPACENAME FROM DBA_TABLESPACES; -- 创建表空间 CREATE TABLESPACE SPACE DATAFILE ‘E ...

  6. HDOJ 2102 A计划(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 思路分析: <1>搜索方法分析:由于需要寻找最短的找到公主的路径,所以采用bfs搜索 ...

  7. HDU 3032 Nim or not Nim? (sg函数求解)

    Nim or not Nim? Problem Description Nim is a two-player mathematic game of strategy in which players ...

  8. javaweb学习路之四--cxf发布Webservice

    背景:maven构建的springMvc+mybatis框架 源码--->https://github.com/Zering/MyWeb 步骤:(本步骤是自己在实际探索过程中的步骤,我的思路是先 ...

  9. Spring boot实现数据库读写分离

    背景 数据库配置主从之后,如何在代码层面实现读写分离? 用户自定义设置数据库路由 Spring boot提供了AbstractRoutingDataSource根据用户定义的规则选择当前的数据库,这样 ...

  10. 利用UICollectionView实现瀑布流

    利用UICollectionView实现瀑布流通过自定义布局来实现. - 自定义类继承UICollectionViewLayout: 必须重写的方法有: //决定每个item的位置: - (nulla ...