Title: http://blog.csdn.net/wukonwukon/article/details/7945526
Code: http://blog.csdn.net/acm_ted/article/details/7943188
Official: http://codeforces.com/blog/entry/5177
Given the arrangement of two two 1-n, the definition of two ranges is: the minimum value of the distance between all the same digits, more formally, it's such minimum |i-j|, that AI = bj. Define a cyclic shift number I (1 ≤i≤n) of permutation b consisting from n elements is a permutation Bibi + 1 ... Bnb1b2 ... bi-1. Overall a permutation has n cyclic shifts. The distance of the output n a cyclic shifts.
eg
4
2 1 3 4 3 4 2 1
Output 2 1 0 1
220c-little Elephant and shifts
Each of the shifts can is divided into two parts-the right (the one this starts from occurrence 1) and the Rest of the elements). If we could keep minimal distance for each part, the minimal of, and numbers of, the answers for the corresponding SH Ift. Lets solve the problems of the right part, the left would be almost the same.
Let we have some shift, for example 34567[12] and the permutation A are 4312765 and B are 2145673, then shifted B is 4567321 . Let we keep two sets (S1 and S2). The the ' the ' keep all ' distances from integers in the ' corresponding positions in A ' for the Examp Le above, it is \texttt{2, 4}). When you are come to the next shift and all integers in S1 should are decreased by 1 (which is because all distances are also ased by 1). But now some integers in set is negative, when any negative integer occures (it always would be-1) you need to delete It from S1 and put 1 to the S2. Also after shifting to the next shifts, all integers in S2 must is increase by 1. After this, for any shift, the answer would be minimum to the smallest numbers in S1 and S2.
It is very useful to use standart ' set ' in C + +.
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
#define MAX 100010
int A[max],b[max],hashx[max],d[max];
multiset<int> s;
int main ()
{
int n,t,res;
scanf ("%d", &n);
for (int i=0; i<n; ++i)
{
scanf ("%d", a+i);
hashx[a[i]]=i;//holds the position of the number in a for
(int i=0; i<n; ++i)
{
scanf ("%d", b+i);
S.insert (I-hashx[b[i]]);//Insert the distance of the same number
} for
(int i=0; i<n; ++i)
{
multiset<int>:: Iterator It=s.lower_bound (i);//Find the first number that is greater than or equal to I
res=0xfffff;
if (It!=s.end ()) Res=min (res, (*it)-i);//The absolute value of the smallest number in nonnegative numbers
if (It!=s.begin ()) Res=min (res,i-(* (--it)));// The absolute value of the largest number in negative numbers
printf ("%d\n", res);
Analog rotating
t=b[i];
S.erase (S.find (i-hashx[t));
S.insert ((i+n)-hashx[t]);
}
return 0;
}