CF1778B.The Forbidden Permutation

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

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 (1≤ai≤n1 \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}) \lt \mathrm{pos}(a_{i + 1}) \le \mathrm{pos}(a_{i}) + d for all 1≤i<m1 \le i \lt 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) \lt \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).

给你一个长度为 nn 的排列 pp,一个由 mm 个互不相同的整数构成的数组 a1,a2,…,ama_1, a_2, \ldots, a_m(其中 1≤ai≤n1 \le a_i \le n),以及一个整数 dd。

令 pos(x)\mathrm{pos}(x) 表示元素 xx 在排列 pp 中的下标(位置)。若对所有 1≤i<m1 \le i < m 均满足

pos(ai)<pos(ai+1)≤pos(ai)+d,\mathrm{pos}(a_{i}) \lt \mathrm{pos}(a_{i + 1}) \le \mathrm{pos}(a_{i}) + d,

则称数组 aa 不是好数组(即“not good”)。

例如,对于排列 p=[4,2,1,3,6,5]p = [4, 2, 1, 3, 6, 5] 和 d=2d = 2:

  • a=[2,3,6]a = [2, 3, 6] 是一个不是好数组;
  • a=[2,6,5]a = [2, 6, 5] 是好数组,因为 pos(a1)=2\mathrm{pos}(a_1) = 2,pos(a2)=5\mathrm{pos}(a_2) = 5,不满足 pos(a2)≤pos(a1)+d\mathrm{pos}(a_2) \le \mathrm{pos}(a_1) + d;
  • a=[1,6,3]a = [1, 6, 3] 是好数组,因为 pos(a2)=5\mathrm{pos}(a_2) = 5,pos(a3)=4\mathrm{pos}(a_3) = 4,不满足 pos(a2)<pos(a3)\mathrm{pos}(a_2) \lt \mathrm{pos}(a_3)。

每次操作允许你交换排列 pp 中两个相邻的元素。问:使数组 aa 成为好数组所需的最少操作次数是多少?可以证明,总存在一系列操作使得 aa 变为好数组。

排列是指由 11 到 nn 的 nn 个互不相同整数按任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列;而 [1,2,2][1,2,2] 不是排列(数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \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 (2≤n≤1052\leq n \leq 10^5, 2≤m≤n2\leq m\leq n, 1≤d≤n1 \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 (1≤pi≤n1\leq p_i \leq n, pi≠pjp_i \ne p_j for i≠ji \ne j).

The third line contains mm distinct integers a1,a2,…,ama_1, a_2, \ldots, a_m (1≤ai≤n1\leq a_i \leq n, ai≠aja_i \ne a_j for i≠ji \ne j).

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含三个整数 nn、mm 和 dd(2≤n≤1052\leq n \leq 10^5,2≤m≤n2\leq m\leq n,1≤d≤n1 \le d \le n),分别表示排列 pp 的长度、数组 aa 的长度以及 dd 的值。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1\leq p_i \leq n,当 i≠ji \ne j 时 pi≠pjp_i \ne p_j)。

第三行包含 mm 个互不相同的整数 a1,a2,…,ama_1, a_2, \ldots, a_m(1≤ai≤n1\leq a_i \leq n,当 i≠ji \ne j 时 ai≠aja_i \ne a_j)。

所有测试用例中 nn 的总和不超过 5⋅1055 \cdot 10^5。

输出格式

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

对于每个测试用例,输出使数组 aa 变为“好”数组所需的最少移动次数。

输入输出样例

  • 输入#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) \lt \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) \lt \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.

第一种情况:pos(a1)=1pos(a_1)=1,pos(a2)=3pos(a_2)=3。为使数组 aa 变为“好”的,一种方法是交换 p3p_3 与 p4p_4。交换后,数组 aa 将变为“好”的,因为条件 pos(a2)≤pos(a1)+d\mathrm{pos}(a_2) \le \mathrm{pos}(a_1) + d 将不再满足。

第二种情况:pos(a1)=1pos(a_1)=1,pos(a2)=4pos(a_2)=4。这 3 次操作可以是:

  1. 交换 p3p_3 与 p4p_4;
  2. 交换 p2p_2 与 p3p_3;
  3. 交换 p1p_1 与 p2p_2。

经过这些操作后,排列 pp 将变为 [2,5,4,3,1][2,5,4,3,1]。此时数组 aa 将变为“好”的,因为条件 pos(a1)<pos(a2)\mathrm{pos}(a_1) \lt \mathrm{pos}(a_2) 将不再满足。可以证明:无法用少于 3 次操作使数组 aa 变为“好”的。

第三种情况:pos(a1)=1pos(a_1)=1,pos(a2)=3pos(a_2)=3,pos(a3)=5pos(a_3)=5。这 2 次操作可以是:

  1. 交换 p4p_4 与 p5p_5;
  2. 交换 p3p_3 与 p4p_4。

经过这些操作后,排列 pp 将变为 [3,4,2,1,5][3,4,2,1,5]。此时数组 aa 将变为“好”的,因为条件 pos(a2)<pos(a3)\mathrm{pos}(a_2) \lt \mathrm{pos}(a_3) 将不再满足。可以证明:无法用少于 2 次操作使数组 aa 变为“好”的。

第四种情况:pos(a1)=2pos(a_1)=2,pos(a2)=1pos(a_2)=1。数组 aa 已经是“好”的。

第五种情况:pos(a1)=2pos(a_1)=2,pos(a2)=5pos(a_2)=5。这 2 次操作是:

  1. 交换 p1p_1 与 p2p_2;
  2. 交换 p5p_5 与 p6p_6。

输入解题思路,AI测评打分。不知道怎么写?

首页