CF1778B.The Forbidden Permutation
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation p of length n, an array of m distinct integers a1,a2,…,am (1≤ai≤n), and an integer d.
Let pos(x) be the index of x in the permutation p. The array a is not good if
- pos(ai)<pos(ai+1)≤pos(ai)+d for all 1≤i<m.
For example, with the permutation p=[4,2,1,3,6,5] and d=2:
- a=[2,3,6] is a not good array.
- a=[2,6,5] is good because pos(a1)=2, pos(a2)=5, so the condition pos(a2)≤pos(a1)+d is not satisfied.
- a=[1,6,3] is good because pos(a2)=5, pos(a3)=4, so the condition pos(a2)<pos(a3) is not satisfied.
In one move, you can swap two adjacent elements of the permutation p. What is the minimum number of moves needed such that the array a becomes good? It can be shown that there always exists a sequence of moves so that the array a becomes good.
A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3, but there is 4 in the array).
给你一个长度为 n 的排列 p,一个由 m 个互不相同的整数构成的数组 a1,a2,…,am(其中 1≤ai≤n),以及一个整数 d。
令 pos(x) 表示元素 x 在排列 p 中的下标(位置)。若对所有 1≤i<m 均满足
pos(ai)<pos(ai+1)≤pos(ai)+d,
则称数组 a 不是好数组(即“not good”)。
例如,对于排列 p=[4,2,1,3,6,5] 和 d=2:
- a=[2,3,6] 是一个不是好数组;
- a=[2,6,5] 是好数组,因为 pos(a1)=2,pos(a2)=5,不满足 pos(a2)≤pos(a1)+d;
- a=[1,6,3] 是好数组,因为 pos(a2)=5,pos(a3)=4,不满足 pos(a2)<pos(a3)。
每次操作允许你交换排列 p 中两个相邻的元素。问:使数组 a 成为好数组所需的最少操作次数是多少?可以证明,总存在一系列操作使得 a 变为好数组。
排列是指由 1 到 n 的 n 个互不相同整数按任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列;而 [1,2,2] 不是排列(数字 2 出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains three integers n, m and d (2≤n≤105, 2≤m≤n, 1≤d≤n), the length of the permutation p, the length of the array a and the value of d.
The second line contains n integers p1,p2,…,pn (1≤pi≤n, pi=pj for i=j).
The third line contains m distinct integers a1,a2,…,am (1≤ai≤n, ai=aj for i=j).
The sum of n over all test cases doesn't exceed 5⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含三个整数 n、m 和 d(2≤n≤105,2≤m≤n,1≤d≤n),分别表示排列 p 的长度、数组 a 的长度以及 d 的值。
第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n,当 i=j 时 pi=pj)。
第三行包含 m 个互不相同的整数 a1,a2,…,am(1≤ai≤n,当 i=j 时 ai=aj)。
所有测试用例中 n 的总和不超过 5⋅105。
输出格式
For each test case, print the minimum number of moves needed such that the array a becomes good.
对于每个测试用例,输出使数组 a 变为“好”数组所需的最少移动次数。
输入输出样例
输入#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)=1, pos(a2)=3. To make the array good, one way is to swap p3 and p4. After that, the array a will be good because the condition pos(a2)≤pos(a1)+d won't be satisfied.
In the second case, pos(a1)=1, pos(a2)=4. The 3 moves could be:
- Swap p3 and p4.
- Swap p2 and p3.
- Swap p1 and p2.
After these moves, the permutation p will be [2,5,4,3,1]. The array a will be good because the condition pos(a1)<pos(a2) won't be satisfied. It can be shown that you can't make the array a good with fewer moves.
In the third case, pos(a1)=1, pos(a2)=3, pos(a3)=5. The 2 moves can be:
- Swap p4 and p5.
- Swap p3 and p4.
After these moves, the permutation p will be [3,4,2,1,5]. The array a will be good because the condition pos(a2)<pos(a3) won't be satisfied. It can be shown that you can't make the array a good with fewer moves.
In the fourth case, pos(a1)=2, pos(a2)=1. The array a is already good.
In the fifth case, pos(a1)=2, pos(a2)=5. The 2 moves are:
- Swap p1 and p2.
- Swap p5 and p6.
第一种情况:pos(a1)=1,pos(a2)=3。为使数组 a 变为“好”的,一种方法是交换 p3 与 p4。交换后,数组 a 将变为“好”的,因为条件 pos(a2)≤pos(a1)+d 将不再满足。
第二种情况:pos(a1)=1,pos(a2)=4。这 3 次操作可以是:
- 交换 p3 与 p4;
- 交换 p2 与 p3;
- 交换 p1 与 p2。
经过这些操作后,排列 p 将变为 [2,5,4,3,1]。此时数组 a 将变为“好”的,因为条件 pos(a1)<pos(a2) 将不再满足。可以证明:无法用少于 3 次操作使数组 a 变为“好”的。
第三种情况:pos(a1)=1,pos(a2)=3,pos(a3)=5。这 2 次操作可以是:
- 交换 p4 与 p5;
- 交换 p3 与 p4。
经过这些操作后,排列 p 将变为 [3,4,2,1,5]。此时数组 a 将变为“好”的,因为条件 pos(a2)<pos(a3) 将不再满足。可以证明:无法用少于 2 次操作使数组 a 变为“好”的。
第四种情况:pos(a1)=2,pos(a2)=1。数组 a 已经是“好”的。
第五种情况:pos(a1)=2,pos(a2)=5。这 2 次操作是:
- 交换 p1 与 p2;
- 交换 p5 与 p6。
输入解题思路,AI测评打分。不知道怎么写?