codeforces

链接:https://codeforces.com/contest/584/problem/E
思路:我们考虑从左到右枚举,然后找当前点需要放的位置到当前点区间范围内,需要放的位置在交换元素右边的点,让他们交换,一直交换到不可交换位置,复杂度O(n^2)

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

using namespace std;

const int maxn = 2010;

int a[maxn], b[maxn], c[maxn];
int n;
typedef pair<int, int> P;
int ans;
vector<P> res;

int (){
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i++) scanf("%d", &b[i]), c[b[i]] = i;
for(int i = 1; i <= n; i++){
for(int j = i - 1, nowi = i; j >= c[a[nowi]]; j--){
if(c[a[j]] >= nowi){
ans += nowi - j;
res.push_back(P{nowi, j});
swap(a[nowi], a[j]);
nowi = j;
}
}
}
printf("%dn%dn", ans, res.size());
for(auto &it : res) printf("%d %dn", it.first, it.second);
return 0;
}