双塔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. cddiv/数组维护

    题目连接 看代码: #include <set> #include <map> #include <cmath> #include <queue> #i ...

  2. 视频 -> 帧 浅析

    原创:转载请注明出处 关于帧率 首先以下几个概念必须弄清楚 1.一个帧就是一个画面 2.视频有无数个帧组成 3.表达时间的量  CMTime 的定义: typedef struct { CMTimeV ...

  3. mysql中exists的用法介绍

    SELECT c.CustomerId, CompanyName   2 FROM Customers c   3 WHERE EXISTS(   4     SELECT OrderID FROM  ...

  4. 本地php 连接 MySQL

    1. 在D:\xampp\htdocs下创建test.php <?php $dbhost = 'localhost:3306'; //mysql服务器主机地址 $dbuser = 'root'; ...

  5. HDU - 5234 Happy birthday

    Problem Description Today is Gorwin’s birthday. So her mother want to realize her a wish. Gorwin say ...

  6. android 5.0新特性学习--CardView

    CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影.CardView是一个Layout,可以布局其他View. 官网地址:https://de ...

  7. C#入门经典-第15章ListBox,CheckedListBox

  8. Intergate flot with Angular js ——Angular 图形报表

    下面这篇文章最终的结论就是 Flot 插件 结合 Angular 的Directive 来处理 图表的绘制 给出github上的一个demo源码.https://gist.github.com/fly ...

  9. base64编码的图片字节流存入html页面中的显示

    在图片数据中加载到一个img标签,并如下处理 <img src="data:image/png;base64,...

  10. DDMS工具使用(转)

    DDMS工具使用   一.查看进程的堆栈使用情况1.选中你要查看的进程:2.点击“ Update Heap”按钮开启该进程的该项功能,如果单独打开ddms工具,按钮名为“Show heap updat ...