HDU 6076 Security Check DP递推优化
Security Check
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Picture from Wikimedia Commons
Two teams A and B are going to travel by plane. Each team has n players, ranked from 1 to n according to their average performance. No two players in the same team share the same rank. Team A is waiting in queue 1 while team B is waiting in queue 2. Nobody else is waiting for security check.
Little Q is the policeman who manages two queues. Every time he can check one person from one queue, or check one each person from both queues at the same time. He can't change the order of the queue, because that will make someone unhappy. Besides, if two players Ai and Bj are being checked at the same time, satisfying |Ai−Bj|≤k, they will make a lot of noise because their rank are almost the same. Little Q should never let that happen.
Please write a program to help Little Q find the best way costing the minimum time.
In each test case, there are 2 integers n,k(1≤n≤60000,1≤k≤10) in the first line, denoting the number of players in a team and the parameter k.
In the next line, there are n distinct integers A1,A2,...,An(1≤Ai≤n), denoting the queue 1 from front to rear.
Then in the next line, there are n distinct integers B1,B2,...,Bn(1≤Bi≤n), denoting the queue 2 from front to rear.
4 2
2 3 1 4
1 2 4 3
Time 1 : Check A_1.
Time 2 : Check A_2.
Time 3 : Check A_3.
Time 4 : Check A_4 and B_1.
Time 5 : Check B_2.
Time 6 : Check B_3.
Time 7 : Check B_4.
题解:
设定f[i][j] 表示 递推到 a[i], b[j] 时的最少时间
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N = 6e5 + , inf = 1e9; int n, a[N], b[N], k, fos[N], f[][N][];
vector<int > hav1[N],hav2[N]; int dfs(int i,int j) {
if(!i || !j) return j+i;
if(abs(a[i] - b[j]) <= k) {
int& ret = f[i>j?:][i][a[i]-b[j] +k];
if(ret) return ret;
return ret = min(dfs(i-,j),dfs(i,j-))+;
}
int ok;
if(i > j) ok = hav1[i-j][upper_bound(hav1[i-j].begin(),hav1[i-j].end(),i) - hav1[i-j].begin()-];
else ok = hav2[j-i][upper_bound(hav2[j-i].begin(),hav2[j-i].end(),i) - hav2[j-i].begin()-];
return ok?(dfs(ok,j - i + ok) + i - ok):max(i,j);
}
int main() {
int T;cin>>T;while(T--) {
scanf("%d%d",&n,&k);
memset(f,,sizeof(f));
memset(fos,,sizeof(fos));
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= n; ++i) scanf("%d",&b[i]),fos[b[i]] = i;
for(int i = ; i <= *n; ++i)
hav1[i].clear(),hav2[i].clear(),hav1[i].push_back(),hav2[i].push_back();
for(int i = ; i <= n; ++i) {
for(int j = a[i] - k; j <= a[i] + k; ++j) {
if(j < || j > n) continue;
if(i > fos[j]) hav1[i - fos[j]].push_back(i);
else hav2[fos[j] - i].push_back(i);
}
}
for(int i = ; i <= *n; ++i)
hav1[i].push_back(n+),hav2[i].push_back(n+);
for(int i = ; i <= *n; ++i) sort(hav1[i].begin(),hav1[i].end());
for(int i = ; i <= *n; ++i) sort(hav2[i].begin(),hav2[i].end());
printf("%d\n",dfs(n,n));
}
return ;
}
HDU 6076 Security Check DP递推优化的更多相关文章
- 2016多校第4场 HDU 6076 Security Check DP,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6076 题意:现要检查两条队伍,有两种方式,一种是从两条队伍中任选一条检查一个人,第二种是在每条队伍中同 ...
- HDU 6076 - Security Check | 2017 Multi-University Training Contest 4
/* HDU 6076 - Security Check [ DP,二分 ] | 2017 Multi-University Training Contest 4 题意: 给出两个检票序列 A[N], ...
- hdu 6076 Security Check
题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6076 2017 Multi-University Training Contest - Team 4 ...
- HDU Tickets(简单的dp递推)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu2089(数位DP 递推形式)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Cayley-Hamilton定理与矩阵快速幂优化、常系数线性递推优化
原文链接www.cnblogs.com/zhouzhendong/p/Cayley-Hamilton.html Cayley-Hamilton定理与矩阵快速幂优化.常系数线性递推优化 引入 在开始本文 ...
- HDU 3469 Catching the Thief (博弈 + DP递推)
Catching the Thief Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题解报告:hdu 2084 数塔(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...
- hdu 2604 Queuing(dp递推)
昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...
随机推荐
- vim的复制粘贴(包括系统剪贴板)
1.vim常用复制粘贴命令 Vim的复制粘贴命令无疑是y (yank),p(paster),加上yy,P PS: vim有个很有意思的约定(我觉得是一种约定),就是某个命令的大小写都是实现某种功能,只 ...
- 不吹不黑,关于 Java 类加载器的这一点,市面上没有任何一本图书讲到
类加载器第7弹: 实战分析Tomcat的类加载器结构(使用Eclipse MAT验证) 还是Tomcat,关于类加载器的趣味实验 了不得,我可能发现了Jar 包冲突的秘密 重写类加载器,实现简单的热替 ...
- 【Visual Studio】Windows program compatibility mode is on, turn it off……
[问题描述]Windows 10上安装 Visual Studio Ultimate 2013,出现下面错误: [解决方案]将.iso文件解压到硬盘再安装.
- Linux spi驱动分析(二)----SPI核心(bus、device_driver和device)
一.spi总线注册 这里所说的SPI核心,就是指/drivers/spi/目录下spi.c文件中提供给其他文件的函数,首先看下spi核心的初始化函数spi_init(void).程序如下: 点击(此处 ...
- nVidia的物理系统
PhysX PhysX(wiki en 中文,physx wiki physx wiki2)是nVidia公司一款跨平台实时物理引擎,可使用硬件(GPU.PPU: Physics Process ...
- EXT.JS6中的model,store,proxy的一些用法
//one-to-one Ext.define('Address', { extend: 'Ext.data.Model', fields: [ 'address', 'city', 'state', ...
- 公司内部技术分享之Vue.js和前端工程化
今天主要的核心话题是Vue.js和前端工程化.我将结合我这两年多的工作学习经历来谈谈这个,主要侧重点是前端工程化,Vue.js侧重点相对前端工程化,比重不是特别大. Vue.js Vue.js和Rea ...
- 济南day4
啥也不会,做了不对,对了没分.T1 50 + 30 + 0想了想,有思路,写了,半个小时写完,算错复杂度,复杂度最差(n*m),想成了(n+m)被卡没了50分,gg.....T2自己写了个单向并查集, ...
- CompletionService 与 ExecutorService 获取任务执行结果时的区别
CompletionService 与 ExecutorService 之间的区别 在讨论二者之间的区别之前,先交待一下背景. 看了ElasticSearch Transport模块的源码,里面充满了 ...
- NYOJ 38 布线问题_(解法1 Kruskal算法)
时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 南阳理工学院要进行用电线路改造.如今校长要求设计师设计出一种布线方式,该布线方式须要满足下面条件: 1.把全部的楼都供 ...