Responsive image

问题 1829 --Security Check

1829: Security Check

时间限制: 6 Sec  内存限制: 128 MB
提交: 1  解决: 1
[提交][状态][讨论版][命题人:]

题目描述

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.

输入描述

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.
 

输出描述

For each test case, print a single line containing an integer, denoting the minimum time to check all people.

样例输入

1
4 2
2 3 1 4
1 2 4 3

样例输出

7

提示






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.








来源

 

[提交][状态]
ACM算法攻关部