Security Check

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Problem Description
In airport of Bytetown, there are two long queues waiting for security check. Checking a person needs one minute, and two queues can be checked at the same time.


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.

 
Input
The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.

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.

 
Output
For each test case, print a single line containing an integer, denoting the minimum time to check all people.
 
Sample Input
1
4 2
2 3 1 4
1 2 4 3
 
Sample Output
7

Hint

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递推优化的更多相关文章

  1. 2016多校第4场 HDU 6076 Security Check DP,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6076 题意:现要检查两条队伍,有两种方式,一种是从两条队伍中任选一条检查一个人,第二种是在每条队伍中同 ...

  2. HDU 6076 - Security Check | 2017 Multi-University Training Contest 4

    /* HDU 6076 - Security Check [ DP,二分 ] | 2017 Multi-University Training Contest 4 题意: 给出两个检票序列 A[N], ...

  3. hdu 6076 Security Check

    题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6076 2017 Multi-University Training Contest - Team 4 ...

  4. HDU Tickets(简单的dp递推)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. hdu2089(数位DP 递推形式)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. Cayley-Hamilton定理与矩阵快速幂优化、常系数线性递推优化

    原文链接www.cnblogs.com/zhouzhendong/p/Cayley-Hamilton.html Cayley-Hamilton定理与矩阵快速幂优化.常系数线性递推优化 引入 在开始本文 ...

  7. HDU 3469 Catching the Thief (博弈 + DP递推)

    Catching the Thief Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. 题解报告:hdu 2084 数塔(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...

  9. hdu 2604 Queuing(dp递推)

    昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...

随机推荐

  1. bzoj 2330 [SCOI2011]糖果 差分约束模板

    题目大意 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配 ...

  2. 我要好offer之 链表大总结

    单链表是一种递归结构,可以将单链表看作特殊的二叉树(我把它叫做一叉树) 单链表的定义: /** * Definition for singly-linked list. * struct ListNo ...

  3. Linux中将一个GBK编码的文件转换成UTF-8编码文件

    Linux中将一个GBK编码的文件转换成UTF-8编码文件 使用iconv 命令iconv -f GBK -t UTF-8 file1 -o file2 输出另一个文件,然后再覆盖源文件内容

  4. Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80

    ubuntu上安装Apache2时出现错误 Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0: ...

  5. rp6818 和rp4418 重点修改的kernel几个问题!

    kernel\arch\arm\plat-s5p6818\drone\include文件下#define PAD_GPIOD1     (PAD_MODE_ALT | PAD_FUNC_ALT1 | ...

  6. jquery 分页功能

    <div class="wrapper"> <div class="row"> <div class="col-sm-1 ...

  7. PAT 甲级 1045 Favorite Color Stripe(DP)

    题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...

  8. 分享Kali Linux 2017年第11周镜像文件

     分享Kali Linux 2017年第11周镜像文件 Kali?Linux官方于3月12日发布2017年的第11周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KDE ...

  9. Java 对象的序列化、反序列化

    对象的序列化(Serialize):将内存中的Java对象转换为与平台无关的二进制流(字节序列),然后存储在磁盘文件中,或通过网络传输给另一个网络节点. 对象的反序列化(Deserialize):获取 ...

  10. 老哥你真的知道ArrayList#sublist的正确用法么

    我们有这么一个场景,给你一个列表,可以动态的新增,但是最终要求列表升序,要求长度小于20,可以怎么做? 这个还不简单,几行代码就可以了 public List<Integer> trimL ...