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. Linux System Programming 学习笔记(十一) 时间

    1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...

  2. FZOJ Problem 2110 Star

                                                                                                        ...

  3. Django时区配置:有次发现缓存的时间总是有问题,原来是时区配置需要改

    # LANGUAGE_CODE = 'en-us' # TIME_ZONE = 'UTC' LANGUAGE_CODE = 'zh-Hans' TIME_ZONE = 'Asia/Shanghai'

  4. 转载 关于malloc

    1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败,则返回一个空指针(NULL). 关于分配失败 ...

  5. ../wxs/utils.wxs not found from

    ../wxs/utils.wxs not found from 微信小程序,使用Vant Weapp时,引入到项目中时报以下错误: ... ../wxs/utils.wxs not found fro ...

  6. LeetCode OJ--Best Time to Buy and Sell Stock II

    http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 第二问,是说可以进行无数次买卖. 贪心法 #include &l ...

  7. Understanding Linux CPU Load - when should you be worried?

    http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages

  8. 如何让一个现有的程序集运行在Silverlight环境中

    故事是这样的:我们有一个组件,是一个标准的Class Library,里面有一些代码是实现了某些计算或者业务逻辑.例如下面这样 然后,我们做了一个Silverlight的应用程序,和一个用于运行该程序 ...

  9. HDU 5046 Airport【DLX重复覆盖】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意: 给定n个城市的坐标,要在城市中建k个飞机场,使城市距离最近的飞机场的最长距离最小,求这 ...

  10. [HEOI2015]定价

    题目描述 在市场上有很多商品的定价类似于 999 元.4999 元.8999 元这样.它们和 1000 元.5000 元和 9000 元并没有什么本质区别,但是在心理学上会让人感觉便宜很多,因此也是商 ...