B. Swaps
原创
©著作权归作者所有:来自51CTO博客作者whiteawll的原创作品,请联系作者获取转载授权,否则将追究法律责任
https://codeforces.com/contest/1573/problem/B
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
typedef long long ll;
typedef pair<char, int > PII;
int n;
int cmp (vector<int> a, vector<int> b) {
}
void solve() {
cin >> n;
vector<int> a(n * 2 + 1), b(n * 2 + 1);
int minn = 0x3f3f3f3f;
for (int i = 1; i<= n; i++) {
int t;
cin >>t;
a[t] = i;
}
for (int i =1; i<= n; i ++) {
int t;
cin >> t;
b[t] = i;
}
int ans =0x3f3f3f3f;
for (int i = 2; i <=n * 2; i += 2) {
minn = min (minn, a[i -1]);
ans = min (ans, minn + b[i] - 2);
}
cout << ans << endl;
}
int main () {
int t;
cin >> t;
while (t --) solve();
return 0;
}
只要第一个位置满足条件就行了。为此我们从小到大遍历,然后选择坐标最小的,并且始终满足条件。