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 ...
随机推荐
- flexigrid
一.参考资料 1.jQuery插件flexiGrid的完全使用,附代码下载 2.修改flexigrid源码一(json,checkbox)[原创] 3.jQuery +UI + flexigrid做的 ...
- 使用HttpUtils 上传视频文件
private void shangchuan(){ //文件的路径 //File file=new File(path); File fi ...
- java 线程的中断
Example12_6.java public class Example12_6 { public static void main(String args[]) { ClassRoom room6 ...
- Redis学习笔记(二)-key相关命令【转载】
转自 Redis学习笔记(二)-key相关命令 - 点解 - 博客园http://www.cnblogs.com/leny/p/5638764.html Redis支持的各种数据类型包括string, ...
- ASP.NET 的内置对象
ASP.NET的内置对象介绍 1.Response 2.Request 3.Server 4.Application 5.Session 6.Cookie Request对象主要是让服务器取得客户端浏 ...
- php的表单安全处理
规则 1:绝不要信任外部数据或输入 关于 Web 应用程序安全性,必须认识到的第一件事是不应该信任外部数据.外部数据(outside data) 包括不是由程序员在 PHP 代码中直接输入的任何数据. ...
- HDU 1203 I NEED A OFFER! 01背包 概率运算预处理。
题目大意:中问题就不说了 ^—^~ 题目思路:从题目来看是很明显的01背包问题,被录取的概率记为v[],申请费用记为w[].但是我们可以预先做个处理,使问题解决起来更方便:v[]数组保留不被录取的概率 ...
- Android中的自定义Adapter(继承自BaseAdapter)——与系统Adapter的调用方法一致——含ViewHolder显示效率的优化(转)
Android中很多地方使用的是适配器(Adapter)机制,那我们就要好好把这个Adapter利用起来,并且用出自己的特色,来符合我们自行设计的需要喽~~~ 下面先上一个例子,是使用ViewHold ...
- Naive Bayes在mapreduce上的实现(转)
Naive Bayes在mapreduce上的实现 原文地址 http://www.cnblogs.com/sunrye/p/4553732.html Naive Bayes是比较常用的分类器,因为思 ...
- Bootstrap 容器(Container)及网格说明-(二)
1.容器(Container) 2.网格 来自为知笔记(Wiz)