ZOJ 3331 Process the Tasks
双塔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的更多相关文章
- 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 ...
- ZOJ 3331 Process the Tasks 双塔Dp
用dp[i][j]表示当前安排好了前i个任务,且机器A和机器B完成当前分配到的所有任务的时间差为j(这里j可正可负,实现的时候需要加个offset)时,完成这些任务的最早时间.然后根据j的正负,分别考 ...
- java多线程系类:JUC线程池:03之线程池原理(二)(转)
概要 在前面一章"Java多线程系列--"JUC线程池"02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包 ...
- Java多线程系列--“JUC线程池”03之 线程池原理(二)
概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...
- Celery - Best Practices
If you've worked with Django at some point you probably had the need for some background processing ...
- Chrome中的消息循环
主要是自己做个学习笔记吧,我经验也不是很丰富,以前学习多线程的时候就感觉写多线程程序很麻烦.主要是线程之间要通信,要切线程,要同步,各种麻烦.我本身的工作经历决定了也没有太多的工作经验,所以chrom ...
- 在top命令下kill和renice进程
For common process management tasks, top is so great because it gives an overview of the most active ...
- activiti总结
1.activiti如何修改登录用户名?在哪个数据库里面添加. 2.activiti的启动和部署在http://activiti.org/userguide/index.html#demo.setup ...
- Java并发编程--线程池
1.ThreadPoolExecutor类 java.uitl.concurrent.ThreadPoolExecutor类是线程池中最核心的一个类,下面我们来看一下ThreadPoolExecuto ...
随机推荐
- liunx 定时执行 php文件
which php 寻找php路径
- servlet中路径的获取
1.获取项目的绝对路径 可以request.getRealPath("/"),但是这个方法已经废弃了,最好用this.getServletContext().getRealPath ...
- UIImagePikerController 浅析
原文链接:http://www.jianshu.com/p/2ac85aca4468 UIImagePickerController是iOS系统提供的和系统的相册和相机交互的一个类,可以用来获取相册的 ...
- hdu 2612(Find a way)(简单广搜)
Find a way Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- java数组的引用
数组属于应用型变量,因此两个相投类型的数组如果具有相同的引用,它们就有完全相同的元素 如: int a[]={1,2,3},b[]={4,5} 如果a=b;则a[]={4,5} public clas ...
- VS2013编译FileZilla0.9.44
2014年,FileZilla更新了一下,到了44版本了,貌似也是用VS2013的工程做的项目,所以下载了server的安装包,然后安装SourceCode即可(需要安装InterFace,是安装必选 ...
- 编译filezilla
编译zilla的时候,需要用到与mysql连接的地方(这里先忽略zila的编译) VC听过mysql connector c++, 下载了1.1.3版本,然后飞安装包,之后从官网上下载boost 把库 ...
- Linux系统编程读书笔记
文件I/O模型 Linux的哲学思想,一切皆文件,这也是Linux文件操作的方便之处.系统调用不会分配缓冲区用以返回信息给调用者.所以必须提前分配大小合适的缓冲区并将缓冲区指针传递给系统调用. 1.o ...
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)
C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...
- centos和ubuntu下使用cron设置定时任务
1.启动cron工具[ps:使用root权限] centos启动cron两种方式 a) /etc/init.d/crond start b) service crond start ubuntu启动c ...