CF1778B.The Forbidden Permutation
普及/提高-
通过率:0%
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).
输入格式
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 .
输出格式
For each test case, print the minimum number of moves needed such that the array a 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)=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 .