题目链接:
题意:
给定长度为n的两个全排列A和B,可对全排列的逆序对进行交换,问A可否通过若干次交换得到B,若可以,输出交换次数和
思路:
①从左往右按下标遍历,若 a[i]=b[i] ,则无需再调整。
②若当前 a[i]<b[i],则直接没有答案,因为是操作逆序对,向后考虑操作只能换来更小的值,不可能匹配上。
③若a[i]>b[i],我们是不能直接交换a[i]和在a数组中等于b[i]的位置的数的,比如以下两个序列。
A 5 3 4 2 1
B 2 5 4 3 1
如果我在a中直接交换5和2,会得到序列2 3 4 5 1,这样接下来是无法交换5和3的,这是因为直接交换破坏了原来5和3的先后顺序,在从5到2交换的路上,所有碰到的5到2的递减序列,都倒着交换,这里就是5 3 2,倒着交换就是3先和2交换,2再和5交换,得到2 5 4 3 1。
样例:
输入:
3
2
1 2
2 1
4
4 1 2 3
1 3 2 4
8
8 7 6 5 4 3 2 1
1 8 7 6 5 4 3 2
输出:
-1
2
1 2
2 4
7
7 8
6 7
5 6
4 5
3 4
2 3
1 2
代码:
#include <bits/stdc++.h>
using namespace std;
typedef struct Node{
int l,r;
}Node;
const int N=2025;
int a[N],b[N],pos[N];
int t,n;
queue<Node> q;
stack<int> st;
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
pos[a[i]]=i;
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
bool flag=true;
for(int i=1;i<=n;i++){
if(a[i]==b[i])
continue;
else if(a[i]<b[i]){
flag=false;
break;
}
else{
int pre=a[i],ed=pos[b[i]];
st.push(i);
for(int j=i+1;j<ed;j++){
if(a[j]<pre&&a[j]>b[i]){
swap(pre,a[j]);
pos[a[j]]=j;
st.push(j);
}
}
a[ed]=pre;
pos[a[ed]]=ed;
a[i]=b[i];
pos[a[i]]=i;
st.push(ed);
int l,r;
while(st.size()!=2){
r=st.top();
st.pop();
l=st.top();
q.push({l,r});
}
r=st.top();
st.pop();
l=st.top();
st.pop();
q.push({l,r});
}
}
if(flag){
printf("%d\n",q.size());
while(!q.empty()){
Node tp=q.front();
q.pop();
printf("%d %d\n",tp.l,tp.r);
}
}
else{
while(!q.empty()){
q.pop();
}
printf("-1\n");
}
}
}