"Title Description
Description "
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.
Enter a description
Input Description
A total of three rows, the first line contains an integer n, indicating the number of matches in each box.
The second line has n integers, separated by a space between each two integers, representing the height of the first row of matches.
The third line has n integers, separated by a space between each two integers, indicating the height of the second row of matches.
Output description
Output Description
Outputs a common line that contains an integer that represents the result of a minimum number of interchanges for 99,999,997 modulo.
Sample input
Sample Input
[Sample 1]
4
2 3 1 4
3 2 1 4
[Sample 2]
4
1 3 4 2
1 7 2 4
Sample output
Sample Output
[Sample 1]
1
[Sample 2]
2
Data range and Tips
Data Size & Hint
"Sample 1 description"
The minimum distance is 0, which needs to be exchanged at least 1 times, such as exchanging the first 2 matches of the 1th column or exchanging the first 2 matches of the 2nd column.
"Sample 2 description"
The minimum distance is 10, the minimum need to exchange 2 times, such as: Swap the position of the Middle 2 matches in the 1th column, and then swap the position of the 2 matches in the 2nd column.
"Data Range"
For 10% of data, 1≤n≤10;
For 30% of data, 1≤n≤100;
For 60% of data, 1≤n≤1,000;
For 100% of data, 1≤n≤100,000,0≤ matches are highly ≤2^31-1.
"Problem-solving ideas"
It is clear that the value of one by one corresponds to the minimum value after sorting
At the beginning of the problem I was to sort a, and then to change B according to a and then reverse the order, this is not correct, because B is also out of order, if the B according to a method of reverse order can not get the correct solution.
A, B should be ordered, and then according to the label of the second to deal with a, is another C array, the array variable is a ordinal number, the group stores the corresponding position B of the label, indicating the need to move the a[i] element to the J position, and then a re-label after the data for reverse.
1 ProgramHuochai;2 typeAa=Record3 No,sum:longint;4 End;5zz=Array[0..100000] ofAA;6 Constmo=99999997;7 varN,i,ans:longint;8A,f:Array[0..100000] ofAA;9B,c,d,e:Array[0..100000] ofLongint;Ten procedureSort (l,r:longint;varc:zz); One var A I,j,x,y:longint; - begin -i:=l; thej:=R; -x:=c[(L+r)Div 2].sum; - - Repeat + whileC[i].sum<x Do - Inc (i); + whileX<c[j].sum Do A Dec (j); at if not(I>J) Then - begin -c[0]:=C[i]; -c[i]:=C[j]; -c[j]:=c[0]; - Inc (i); inj:=j-1; - to End; + - untilI>J; the ifL<j Then * sort (l,j,c); $ ifI<r ThenPanax Notoginseng sort (i,r,c); - End; the + procedureguibing (l,r:longint); A varMid,i,q1,q2,q3:longint; the begin +mid:= (L+r)Div 2; - ifL +1<r Then $ begin $ guibing (l,mid); -Guibing (mid+1, R); - End; the forI:=l toMid Do -c[i]:=B[i];Wuyi forI:=mid+1 toR Do thed[i]:=B[i]; -q1:=l; WuQ2:=mid+1; -q3:=l; About while(q1<mid+1) and(q2<r+1) Do $ begin - ifC[Q1]<D[Q2] Then - begin -e[q3]:=C[Q1]; A Inc (Q1); + Inc (Q3); the End - Else $ begin thee[q3]:=D[Q2]; theAns:= ((mid-q1+1)MoDMo+ans)MoDmo; the Inc (Q3); the Inc (Q2); - End; in End; the ifq2=r+1 Then the forI:=q1 toMid Do About begin thee[q3]:=C[i]; the Inc (Q3); the End + Else - forI:=q2 toR Do the beginBayie[q3]:=D[i]; the Inc (Q3); the End; - forI:=l toR Dob[i]:=E[i]; - the End; the the begin the read (n); - fori:=1 toN Doread (a[i].sum); the fori:=1 toN Do the begin thea[i].no:=i;94f[i].no:=i; the End; the fori:=1 toN Doread (f[i].sum); theSort1, n,a);98Sort1, n,f); About fori:=1 toN Dob[a[i].no]:=f[i].no;//the point is here!!! - 101Guibing (1, n);102 writeln (ans);103 End.
Codevs 3286 matches Line