11.7
也可以不用判环,暴力check,按照顺序暴力跑(看代码吧)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
#define N 400010
using namespace std;
int n, cnt;
int a[N], b[N];
inline int read(){
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
int calc(){
cnt = 0;
for(register int i=0; i<n; i++){
while (b[i] != i){
cnt++; swap(b[i], b[b[i]]);
}
}
return cnt;
}
bool judge(int i){
if(i >= cnt) return 1;
return 0;
}
struct AA{
int x, y;
}aa[N];
int check(int x){
for(register int i=0; i<n; i++) b[i] = a[i];
for(int i=1; i<=x; i++){
swap(b[aa[i].x], b[aa[i].y]);
}
return calc();
}
int main(){
freopen ("c.in", "r", stdin);
freopen ("c.out", "w", stdout);
scanf("%d", &n);
for(register int i=0; i<n; i++) a[i] = read(), b[i] = a[i];
cnt = calc(); if( !cnt ){ printf("0\n"); return 0;}
int ans = 0;
for(register int i=1; i<=2*n; i++)
aa[i].x = read(), aa[i].y = read();
int l = 0, r = n;
while ( l <= r ){
int mid = (l + r) >> 1;
int cc = check( mid );
if(cc <= mid) ans = mid, r = mid - 1;
else l = mid + 1;
}
cout << ans << endl;
}