Han Han has two boxes of matches, each containing n matches, each match has a height. Now the matches in each box are lined up in one column, the same match height is different, and the distance between the two matches is defined as:
, where AI represents the height of the first match in a match, and BI represents the height of the I match in the second row of matches.
The positions of the two adjacent matches in each match can be exchanged, so that the distance between the two matches is minimized by swapping. How many times do I have to exchange the minimum distance? If this number is too large, output the result of this minimum interchange number to 99,999,997 modulo.
Intput:
4
2 3 1 4
3 2 1 4
Output
1
Ideas:
Open a struct first, then enter. Sort by the size of V in ascending order.
1 Const int maxn=100010; 2 struct Cyc 3 {4 int V,id; 5 }A[MAXN],B[MAXN];
Then, store the position of the element in a in the corresponding B to C.
Finally, the merge sort can be done.
1#include <iostream>2#include <string>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <iomanip>8#include <queue>9 using namespacestd;Ten Const intmaxn=100010, lln=99999997; One structCyc A { - intV,id; - }A[MAXN],B[MAXN]; the intN; - intC[MAXN],D[MAXN]; - intans=0; - BOOLMyc (cyc x,cyc y) + { - returnx.v<y.v; + } A voidWorkintLintR) at { - intmid,tmp,i,j; - if(L +1<R) - { -Mid= (L+R)/2; -Work (l,mid-1); in Work (mid,r); -tmp=l; to for(i=l,j=mid;i<=mid-1&&j<=R;) + { - if(c[i]>C[j]) the { *d[tmp++]=c[j++]; $ans=1ll* (ans+mid-i)%Lln;Panax Notoginseng } - Else the { +d[tmp++]=c[i++]; A } the } + if(j<=R) - { $ for(; j<=r;j++) d[tmp++]=C[j]; $ } - Else - { the for(; i<=mid-1; i++) d[tmp++]=C[i]; - }Wuyi for(i=l;i<=r;i++) thec[i]=D[i]; - } Wu Else{ - if(L +1==R) About { $ if(c[l]>C[r]) - { - swap (c[l],c[r]); -ans=1ll* (ans+1)%Lln; A } + } the } - } $ intMain () the { the /*freopen ("2.in", "R", stdin); the freopen ("2.out", "w", stdout);*/ the //Ios::sync_with_stdio (false); -Cin>>N; in for(intI=1; i<=n;i++) the { theCin>>a[i].v; AboutA[i].id=i; the } the for(intI=1; i<=n;i++) the { +Cin>>b[i].v; -B[i].id=i; the }BayiSort (A +1, A +1+n,myc); theSort (b +1, B +1+n,myc); the for(intI=1; i<=n;i++) -c[b[i].id]=a[i].id; -Work1, n); thecout<<ans<<Endl; the return 0; the}View Code
noip2013 Match Sort