CF1778B.The Forbidden Permutation

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a permutation pp of length nn , an array of mm distinct integers a1,a2,,ama_1, a_2, \ldots, a_m ( 1ain1 \le a_i \le n ), and an integer dd .

Let pos(x)\mathrm{pos}(x) be the index of xx in the permutation pp . The array aa is not good if

  • pos(ai)<pos(ai+1)pos(ai)+d\mathrm{pos}(a_{i}) < \mathrm{pos}(a_{i + 1}) \le \mathrm{pos}(a_{i}) + d for all 1i<m1 \le i < m .

For example, with the permutation p=[4,2,1,3,6,5]p = [4, 2, 1, 3, 6, 5] and d=2d = 2 :

  • a=[2,3,6]a = [2, 3, 6] is a not good array.
  • a=[2,6,5]a = [2, 6, 5] is good because pos(a1)=2\mathrm{pos}(a_1) = 2 , pos(a2)=5\mathrm{pos}(a_2) = 5 , so the condition pos(a2)pos(a1)+d\mathrm{pos}(a_2) \le \mathrm{pos}(a_1) + d is not satisfied.
  • a=[1,6,3]a = [1, 6, 3] is good because pos(a2)=5\mathrm{pos}(a_2) = 5 , pos(a3)=4\mathrm{pos}(a_3) = 4 , so the condition pos(a2)<pos(a3)\mathrm{pos}(a_2) < \mathrm{pos}(a_3) is not satisfied.

In one move, you can swap two adjacent elements of the permutation pp . What is the minimum number of moves needed such that the array aa becomes good? It can be shown that there always exists a sequence of moves so that the array aa becomes good.

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation ( 22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation ( n=3n=3 , but there is 44 in the array).

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt ( 1t1041 \le t \le 10^4 ). The description of the test cases follows.

The first line of each test case contains three integers nn , mm and dd ( 2n1052\leq n \leq 10^5 , 2mn2\leq m\leq n , 1dn1 \le d \le n ), the length of the permutation pp , the length of the array aa and the value of dd .

The second line contains nn integers p1,p2,,pnp_1, p_2, \ldots, p_n ( 1pin1\leq p_i \leq n , pipjp_i \ne p_j for iji \ne j ).

The third line contains mm distinct integers a1,a2,,ama_1, a_2, \ldots, a_m ( 1ain1\leq a_i \leq n , aiaja_i \ne a_j for iji \ne j ).

The sum of nn over all test cases doesn't exceed 51055 \cdot 10^5 .

输出格式

For each test case, print the minimum number of moves needed such that the array aa becomes good.

输入输出样例

  • 输入#1

    5
    4 2 2
    1 2 3 4
    1 3
    5 2 4
    5 4 3 2 1
    5 2
    5 3 3
    3 4 1 5 2
    3 1 2
    2 2 1
    1 2
    2 1
    6 2 4
    1 2 3 4 5 6
    2 5

    输出#1

    1
    3
    2
    0
    2

说明/提示

In the first case, pos(a1)=1pos(a_1)=1 , pos(a2)=3pos(a_2)=3 . To make the array good, one way is to swap p3p_3 and p4p_4 . After that, the array aa will be good because the condition pos(a2)pos(a1)+d\mathrm{pos}(a_2) \le \mathrm{pos}(a_1) + d won't be satisfied.

In the second case, pos(a1)=1pos(a_1)=1 , pos(a2)=4pos(a_2)=4 . The 33 moves could be:

  1. Swap p3p_3 and p4p_4 .
  2. Swap p2p_2 and p3p_3 .
  3. Swap p1p_1 and p2p_2 .

After these moves, the permutation pp will be [2,5,4,3,1][2,5,4,3,1] . The array aa will be good because the condition pos(a1)<pos(a2)\mathrm{pos}(a_1) < \mathrm{pos}(a_2) won't be satisfied. It can be shown that you can't make the array aa good with fewer moves.In the third case, pos(a1)=1pos(a_1)=1 , pos(a2)=3pos(a_2)=3 , pos(a3)=5pos(a_3)=5 . The 22 moves can be:

  1. Swap p4p_4 and p5p_5 .
  2. Swap p3p_3 and p4p_4 .

After these moves, the permutation pp will be [3,4,2,1,5][3,4,2,1,5] . The array aa will be good because the condition pos(a2)<pos(a3)\mathrm{pos}(a_2) < \mathrm{pos}(a_3) won't be satisfied. It can be shown that you can't make the array aa good with fewer moves.In the fourth case, pos(a1)=2pos(a_1)=2 , pos(a2)=1pos(a_2)=1 . The array aa is already good.

In the fifth case, pos(a1)=2pos(a_1)=2 , pos(a2)=5pos(a_2)=5 . The 22 moves are:

  1. Swap p1p_1 and p2p_2 .
  2. Swap p5p_5 and p6p_6 .
首页