#bzoj4989. Why Did the Cow Cross the Road
Why Did the Cow Cross the Road
Title Description
Why did the cow cross the road? We may never know the full reason, but it is certain that Farmer John's cows do end up crossing the road quite frequently. In fact, they end up crossing the road so often that they often bump into each-other when their paths cross, a situation Farmer John would like to remedy.Farmer John raises breeds of cows , and each of his fields is dedicated to grazing for one specific breed; for example, a field dedicated to breed can only be used for cows of breed and not of any other breed. A long road runs through his farm. There is a sequence of fields on one side of the road (one for each breed), and a sequence of fields on the other side of the road (also one for each breed). When a cow crosses the road, she therefore crosses between the two fields designated for her specific breed.
Had Farmer John planned more carefully, he would have ordered the fields by breed the same way on both sides of the road, so the two fields for each breed would be directly across the road from each-other. This would have allowed cows to cross the road without any cows from different breeds bumping into one-another. Alas, the orderings on both sides of the road might be different, so Farmer John observes that there might be pairs of breeds that cross. A pair of different breeds is "crossing" if any path across the road for breed aa must int ersect any path across the road for breed .
Farmer John would like to minimize the number of crossing pairs of breeds. For logistical reasons, he figures he can move cows around on one side of the road so the fields on that side undergo a "cyclic shift". That is, for some , every cow re-locates to the field kk fields ahead of it, with the cows in the last kk fields moving so they now populate the first kk fields. For example, if the fields on one side of the road start out ordered by breed as and undergo a cyclic shift by , the new order will be . Please determine the minimum possible number of crossing pairs of breeds that can exist after an appropriate cyclic shift of the fields on one side of the road.
There are two sequences and corresponding to the upper and lower positions, with a length of n, and the two sequences are arranged in an arrangement of n. When , there will be an edge connecting the top and bottom. You can choose sequence or sequence to rotate any steps Rotate two steps, such as to obtain .
Find the logarithm of the minimum intersecting line segment after rotation.
Input format
The first line of input contains N.
The next N lines describe the order, by breed ID, of fields on one side of the road;
The last N lines describe the order, by breed ID, of the fields on the other side of the road.
Output format
Please output the minimum number of crossing pairs of varieties after cyclic shifting of fields on one side of the road (any side can be shifted).
Example
5 5 4 1 3 2 1 3 2 5 4
0
Data scale and agreement
For of the data: .