He said to be greedy little far-fetched.
First, the nature: For the exchange operation, changing the order of operations results unchanged.
Secondly, the answer is monotone, if the operation can be completed in less time k can be completed in the k + 1 operations.
Big deal because you put more than one other operation was carried out just once.
How does it work?
We sweep this series from the beginning, every encounter a mismatch position to put in that position and the current position where the digital exchange just fine.
greedy? Maybe. Nothing wrong.
Operand is 2n, not to open a small array.
cbx said water problem. . . Do not believe the beginning, later found did not seem too difficult.
Do not try too complicated! ! !
Good greedy.
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 int x[200005],n,a[400005],b[400005],p[200005],r[200005]; 5 bool check(int mid){ 6 for(int i=1;i<=n;++i)r[i]=x[i]; 7 for(int i=1;i<=mid;++i)swap(r[a[i]],r[b[i]]); 8 for(int i=1;i<=n;++i)p[r[i]]=i; 9 int cnt=0; 10 for(int i=1;i<=n;++i)if(r[i]!=i)p[r[i]]=p[i],swap(r[i],r[p[i]]),cnt++; 11 return cnt<=mid; 12 } 13 int main(){ 14 scanf("%d",&n); 15 for(int i=1;i<=n;++i)scanf("%d",&x[i]),x[i]++; 16 for(int i=1;i<=n<<1;++i)scanf("%d%d",&a[i],&b[i]),a[i]++,b[i]++; 17 int l=0,r=n<<1; 18 while(l<r-1)if(check(l+r>>1))r=l+r>>1;else l=(l+r>>1)+1; 19 if(check(l))printf("%d\n",l);else printf("%d\n",r); 20 }