The Forbidden Permutation
一,题目
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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).
Input
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≤10^5, 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*10^5.
Output
For each test case, print the minimum number of moves needed such that the array a becomes good.
Example
Input
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
Output
1
3
2
0
2
Note
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.
![](/assets/blank.gif)
二,思路
(1)因为只要保证a数组中有一对和
不满足pos(
)<pos(
)≤pos(
)+d就能够满足答案要求,所以最优方案肯定只改变一对
和
在p数组中的相对位置(pos(
)即ai在p数组中的位置),或者无须改变任何操作(可能a数组已经有一对
和
不满足pos(
)<pos(
)≤pos(
)+d,所以接下来的思路就是如何改变
和
在p数组中的相对位置使得交换数组p中数字的次数最小。
(2)首先,思路一,让pos()>pos(
),仔细一想,就是让
在p中的相对位置在
右边,说白了就是
一直与其他数交换,直到换的
的左边。这时,想要交换次数最小,我们就需要定义一个变量MIN,用来找到所有
和
的中在p数组中的最小相对距离。根据这个思路,我们可以得到最小的交换次数为MIN。
(3)其次,思路二,让pos()>pos(
)+d,当然,这个是不可能的,pos(
)
pos(
)+d,即pos(
)-pos(
)
d,说白了,就是
的相对位置在
左边,且距离至少为d。这时,想要交换次数最小,我们就需要定义一个变量MAX,用来找到所有
和
的中在p数组中的最大相对距离。根据这个思路,我们可以得到的最小交换次数为d-MAX+1(不知道为啥要加1,反正当时看到自己运行结果和答案少了个一就加了一)。
(4)然后,就是一些特判,首先,如果MIN为负数的话,就说明肯定存在一对和
,使得pos(
)-pos(
)>0,既然这样,那就不用交换了,即交换次数为0。其次,如果MAX>d,就说明pos(
)-pos(
)>=d,也是不用交换。最后,就是如果d>n-1的话,pos(
)-pos(
)将不可能大于等于d,所以这种情况下只能用思路一。
(5)那么什么情况下用思路一或二呢,什么情况都用,到最后得到MIN和MAX,我们就判断一下思路一的最小交换次数和思路二哪个交换次数小,然后输出小的。当然,d>n-1的话只能输出MIN。
三,代码
#include<iostream>
#include<cmath>
using namespace std;
#define int long long
#define endl '\n'
int p[200005]={0},a[200005]={0},book[200005]={0};
signed main()
{ios::sync_with_stdio(false); cin.tie(0),cout.tie(0);int t;cin>>t;while(t--){int n,m,d;cin>>n>>m>>d;int MIN=n+1,MAX=-n-1;for(int i=0;i<n;++i) cin>>p[i],book[p[i]]=i+1;//book用于记录a[i]在数组p中的相对距离for(int i=0;i<m;++i) {cin>>a[i];if(i!=0){MIN=min(book[a[i]]-book[a[i-1]],MIN);MAX=max(book[a[i]]-book[a[i-1]],MAX);}}if(MAX>d||MIN<0) cout<<0<<endl;else{if(d-MAX+1<=MIN&&d<n-1) cout<<d-MAX+1<<endl;else cout<<MIN<<endl;}}return 0;
}
The Forbidden Permutation相关推荐
- Minimize the Permutation CodeForces - 1256(贪心)
题意: q次询问,每次询问给你长度为n的排列,然后你每次可以选择一个位置i和i+1的数字进行交换.但是每个位置只能交换一次,问你反转若干次后,这个排列最小是多少? 题目: You are given ...
- C. Paprika and Permutation
C. Paprika and Permutation 问题描述 题意 给你n个数,定义这样一个操作,对于一个数a,任取一个x>0,使得a=a%x.对这n个数至少进行k次这样的操作后如果能得到1~ ...
- 解决github push错误403 Forbidden while accessing
业务场景 我原来在github上创建了一个repository,名称为github_test,我上传了一些文件.但是几年后,我想再次利用该repository,更新并上传文件. 我在一台虚拟机上面gi ...
- 【C++】C++11 STL算法(七):排列操作(Permutation operations)、数值操作(Numeric operations)
排列操作(Permutation operations) 一.is_permutation 1.原型: template< class ForwardIt1, class ForwardIt2 ...
- LeetCode 76. Minimum Window Substring / 567. Permutation in String
76. Minimum Window Substring 典型Sliding Window的问题,维护一个区间,当区间满足要求则进行比较选择较小的字串,重新修改start位置. 思路虽然不难,但是如何 ...
- R语言使用lmPerm包应用于线性模型的置换方法(置换检验、permutation tests)、使用lm模型构建简单线性回归模型、使用lmp函数生成置换检验回归分析模型
R语言使用lmPerm包应用于线性模型的置换方法(置换检验.permutation tests).使用lm模型构建简单线性回归模型.使用lmp函数生成置换检验回归分析模型(Permutation te ...
- R语言使用coin包应用于独立性问题的置换检验(permutation tests、响应变量是否独立于组、两个数值变量是独立的吗、两个分类变量是独立的吗)、以及coin包的常用置换检验函数
R语言使用coin包应用于独立性问题的置换检验(permutation tests.响应变量是否独立于组.两个数值变量是独立的吗.两个分类变量是独立的吗).以及coin包的常用置换检验函数 目录
- R语言使用coin包应用于独立性问题的置换检验(permutation tests)、使用普通cor.test函数和置换近似spearman_test函数、检验变量的相关性的显著性
R语言使用coin包应用于独立性问题的置换检验(permutation tests).使用普通cor.test函数和置换近似spearman_test函数.检验变量的相关性的显著性(correlati ...
- R语言使用coin包应用于分类变量独立性问题的置换检验(permutation tests)、使用普通卡方检验chisq.test函数和置换近似卡方检验chisq.test函数、检验分类变量的独立性
R语言使用coin包应用于分类变量独立性问题的置换检验(permutation tests).使用普通卡方检验chisq.test函数和置换近似卡方检验chisq.test函数.检验分类变量的独立性( ...
最新文章
- MapReduce基础开发之十一DistributedCache使用
- sqlite insert or replace 和 insert or ignore 用法
- java aspose重叠_Aspose.Words - 在特定位置合并两个文档
- 【工具】Jupyter Notebook介绍
- netstat -anp | grep 8199 查看端口占用情况
- json转为tfrecord格式文件怎么转_word怎么转换成pdf格式?这样转很方便
- Fiddler抓取HTTPS最全(强)攻略!
- 如何成为一名汽车软件工程师?
- 怎样关闭vivo的HTML查看器,vivo安全模式在哪儿关闭?
- 思科ccnp认证工程师必看
- 阿里云企业做网站备案流程
- 快捷连接 残差_残差网络解决了什么问题
- 392高校毕业设计选题
- 地理信息可视化大数据系统分析
- 未能配置数据源:未指定“url”属性,无法配置嵌入的数据源。
- 冒充linux内核,4岁小萝莉向Linux内核贡献代码修复「漏洞」而且已经合并到内核...
- 2021届硕士生年初java春招实习面试和正式校招面试经验汇总(收割腾讯,百度,美团,网易等offer)
- html页面控制标签,html常用标签大全
- Java是剑客-飘逸;.NET是刀客-霸道
- sgx是什么要开吗_绝了!滑滑梯设计在顶楼,上去一滑不就是直接跳…楼…吗??...
热门文章
- 古往今来,邮件保密技术之时代变迁
- 学习记录:在office 2019(家庭和学生版)的基础上,安装Visio2019(亲自装过这个版本)
- 欢迎大家my blog in the CSDN RDF instruction for JAVA
- 数据错误循环冗余检查是什么意思_“DCS系统组态”是什么意思?其步骤方法又有哪些?...
- Java开发自学技巧!链表反转的两种实现方法,太香了
- 美国人的地下室里有着最大的脑洞
- linux常用命令,java核心编程百度网盘
- ESP32-VSCODE环境下添加组件,并解决头文件无法找到问题
- Git上传项目三部曲
- 鼎捷软件:“数字化转型”喧嚣下,企业管理者们需要冷思考