双塔DP。

#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std; const int maxn=+;
int T,n;
int dp[maxn][*maxn];
int a[maxn],b[maxn]; void init()
{
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
dp[i][j]=0x7fffffff;
} void read()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
} void work()
{
dp[][a[]+]=dp[][-b[]+]=; for(int i=;i<=n;i++)
{
for(int j=;j<=;j++)
{
if(dp[i-][j]==0x7fffffff) continue;
int tmp=j-;
if(tmp>=)
{
dp[i][a[i]+]=min(dp[i][a[i]+],dp[i-][j]+tmp);
if(b[i]>=tmp) dp[i][tmp-b[i]+]=min(dp[i][tmp-b[i]+],dp[i-][j]+tmp);
else if(b[i]<tmp) dp[i][tmp-b[i]+]=min(dp[i][tmp-b[i]+],dp[i-][j]+b[i]);
} else if(tmp<)
{
tmp=-tmp;
if(dp[i-][j]+tmp<dp[i][-b[i]+]) dp[i][-b[i]+]=min(dp[i][-b[i]+],dp[i-][j]+tmp);
if(a[i]<=tmp) dp[i][a[i]-tmp+]=min(dp[i][a[i]-tmp+],dp[i-][j]+a[i]);
else if(a[i]>tmp) dp[i][a[i]-tmp+]=min(dp[i][a[i]-tmp+],dp[i-][j]+tmp);
}
}
} int ans=0x7fffffff;
for(int j=;j<=;j++)
{
if(dp[n][j]==0x7fffffff) continue;
ans=min(ans,dp[n][j]+abs(j-));
}
printf("%d\n",ans);
} int main()
{
scanf("%d",&T);
while(T--)
{
read();
init();
work();
}
return ;
}

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

  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

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

  3. java多线程系类:JUC线程池:03之线程池原理(二)(转)

    概要 在前面一章"Java多线程系列--"JUC线程池"02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包 ...

  4. Java多线程系列--“JUC线程池”03之 线程池原理(二)

    概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...

  5. Celery - Best Practices

    If you've worked with Django at some point you probably had the need for some background processing ...

  6. Chrome中的消息循环

    主要是自己做个学习笔记吧,我经验也不是很丰富,以前学习多线程的时候就感觉写多线程程序很麻烦.主要是线程之间要通信,要切线程,要同步,各种麻烦.我本身的工作经历决定了也没有太多的工作经验,所以chrom ...

  7. 在top命令下kill和renice进程

    For common process management tasks, top is so great because it gives an overview of the most active ...

  8. activiti总结

    1.activiti如何修改登录用户名?在哪个数据库里面添加. 2.activiti的启动和部署在http://activiti.org/userguide/index.html#demo.setup ...

  9. Java并发编程--线程池

    1.ThreadPoolExecutor类 java.uitl.concurrent.ThreadPoolExecutor类是线程池中最核心的一个类,下面我们来看一下ThreadPoolExecuto ...

随机推荐

  1. UVALive 7291 Kinfolk(最近公共祖先)

    题目中的描述就很最近公共祖先,再说其实这个题并不难,就是麻烦点(代码其实可以化简的),我写的判定比较多. 方法:求出两者的最近公共祖先lca,在求出两者到lca的距离 分析:给出a和b,如果LCA(a ...

  2. javascript预编译

    刚学前端的小白,第一次写博客,难免有点幼稚.以后每周写两次博客,慢慢积累. 笨鸟不必先飞,但一定是最后一个留下的.加油! JS的预编译定义 在一段程序执行前,js会把var和function这两个关键 ...

  3. [转]solr DataImportHandler 解决mysql 表导入内存溢出问题

    最近一个项目要用到solr做全文检索,开始盲人摸象. 用tomcat 7 开始配置,开始正常,但是遇到cookie里有中文就报错. 无奈,换tomcat 6, 结果DataImportHandler ...

  4. Leetcode389

    Find the Difference Given two strings s and t which consist of only lowercase letters. 给出两个字符串,s和t,都 ...

  5. 使用nginx简单实现负载均衡

    只是简单使用nginx玩玩而已,知道能这么用,但是在实际项目中并没有实践过,在项目不大的时候用不到,但是对于理解负载均衡来说还是可以的. 利用虚拟机安装了三个centOS系统,然后顺便装了环境. 这里 ...

  6. DP CF 319 div1B

    http://codeforces.com/contest/319/problem/B 题目大意: 有删除操作,每次都删除数组右边比自己小的.且紧挨着自己的数字.问最小需要删除几次. 思路: 我们定义 ...

  7. Ubuntu防火墙ufw安装配置

    ubuntu 系统默认已安装ufw. 1.安装sudo apt-get install ufw2.启用sudo ufw enablesudo ufw default deny运行以上两条命令后,开启了 ...

  8. android 线程池

    http://blog.csdn.net/wangwenhui11/article/details/6760474 http://blog.csdn.net/cutesource/article/de ...

  9. call_create_syn.sql

    promptprompt ================================================================================prompt ...

  10. 1209:Catch That Cow(bfs)

    题意: 从一个坐标到另一个坐标的移动方式有三种,即:st-1,st+1,2*st.每移动一步时间是一秒. 给出两个坐标,求得从第一坐标到第二座标的最短时间. #include<iostream& ...