Little Elephant and Shifts(CF-220C)

Problem Description

The Little Elephant has two permutations a and b of length n, consisting of numbers from 1 to n, inclusive. Let's denote the i-th (1 ≤ i ≤ n) element of the permutation a as ai, the j-th (1 ≤ j ≤ n) element of the permutation b — as bj.

The distance between permutations a and b is the minimum absolute value of the difference between the positions of the occurrences of some number in a and in b. More formally, it's such minimum |i - j|, that ai = bj.

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 Little Elephant wonders, for all cyclic shifts of permutation b, what is the distance between the cyclic shift and permutation a?

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the size of the permutations. The second line contains permutation a as n distinct numbers from 1 to n, inclusive. The numbers are separated with single spaces. The third line contains permutation b in the same format.

Output

In n lines print n integers — the answers for cyclic shifts. Print the answers to the shifts in the order of the shifts' numeration in permutation b, that is, first for the 1-st cyclic shift, then for the 2-nd, and so on.

Examples

Input

2
1 2
2 1

Output

1
0

Input

4
2 1 3 4
3 4 2 1

Output

2
1
0
1

题意:给出两个长度为 n 的序列 a 、b,定义距离为:若 a[i]==b[i],则 dis=|i-j|,现在要进行 n-1 次操作,每次将 b 序列进行循环左移,要求输出初始序列以及每次左移后的序列的最小的 dis

思路:

若 b[j] 在 a[i] 的左边,那么每次移动,dis+1,反之,若 b[j] 在 a[i] 的右边,每次移动,dis-1

因此使用 multiset 模拟每次挪动的过程,统计每一次左移一共加了多少、减了多少,然后在每次的答案中找大于 0 中最小的,小于 0 中最大的,两者再取最小

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

multiset<int> st;
int bucket[N],b[N];
int main(){
    int n,x;
    scanf("%d",&n);

    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        bucket[x]=i;
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
        int pos=i-bucket[b[i]];
        st.insert(pos);
    }

    multiset<int>::iterator it;
    for(int i=0;i<n;i++){
        it=st.lower_bound(i);//寻找位置
        int res=INF;

        if(it!=st.end()){
            int temp=(*it)-i;
            res=min(res,temp);
        }
        if(it!=st.begin()){
            it--;
            int temp=i-(*it);
            res=min(res,temp);
        }

        printf("%d\n",res);

        int pos=b[i+1];
        int temp=i+1-bucket[pos];
        it=st.find(temp);
        st.erase(it);//删除第一个

        temp+=n;
        st.insert(temp);//插入到最后
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值