두 줄에 각각 n명이 서 있고 한 분에 한 명씩 또는 양쪽에서 한 명씩 동시에 검사할 수 있을 때, 순위 차가 k 이하인 두 사람이 동시에 검사되지 않도록 하는 최소 시간을 구한다.
보통7동적 계획법투 포인터아직 제출이 없습니다시간 제한1초메모리 제한512 MBIn the airport of Bytetown, there are two long queues waiting for the security check. Checking a person takes one minute, and the two queues can be checked at the same time.
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 the security check.
Little Q is the policeman who manages two queues. Every minute, he can either check the first person from one of the queues, or check the first persons from both queues at the same time. He can't change the order in the queues because that will make people unhappy. There is an additional complication, however: if two players A_i and B_j are being checked at the same time, and their ranks are almost the same, specifically ∣A_i−B_j∣≤k, they will make a lot of noise. Little Q should never let that happen.
Please write a program to help Little Q find a way to check all the people so that the required time is minimum possible.
The first line of the input contains two integers n and k: the number of players in each team and the rank similarity parameter (1≤n≤6⋅104, 1≤k≤10).
The second line contains n distinct integers A_1,A_2,…,A_n: the first queue from front to rear (1≤A_i≤n).
The third line contains n distinct integers B_1,B_2,…,B_n: the second queue from front to rear (1≤B_i≤n).
Print a single line containing a single integer: the minimum time required to check all the people.
One possible solution is as follows.
Minute 1: check A_1.
Minute 2: check A_2.
Minute 3: check A_3.
Minute 4: check A_4 and B_1.
Minute 5: check B_2.
Minute 6: check B_3.
Minute 7: check B_4.