E. Little Elephant and Shifts
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ndistinct 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 题意:给出一个1——n的排列a,再给出一个1——n的排列b。若a[i]==b[j],则dis=|i-j|
每次将b序列左移1位,移n-1次,序列第一个补到最后面,问初始序列以及每次左移后的序列最小的dis 若b[j]在a[i]的左边,随着每次左移,两点间dis+1
若b[j]在a[i]的右边,随着每次左移,两点间dis-1 可以统计到这一次左移,一共加了多少1,减了多少1,出队入队的时候再考虑这些1,
即延迟标记
每次的答案,从>=0的里面找最小的,<0的里面找最大的,两者再取最小
multiset 模拟每次把第一个挪到最后一个的过程 解释最后一行:n-a[x]是第一个拿到最后一个的实际距离,再加i+1是延迟标记
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
multiset<int>s;
multiset<int>::iterator it;
int a[],ans,b[];
int main()
{
int n,x;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
a[x]=i;
}
for(int i=;i<=n;i++)
{
scanf("%d",&b[i]);
s.insert(i-a[b[i]]);
}
for(int i=;i<n;i++)
{
it=s.lower_bound(i);
ans=1e5+;
if(it!=s.end()) ans=min(ans,*it-i);
if(it!=s.begin()) ans=min(ans,i-(*--it));
printf("%d\n",ans);
x=b[i+];
s.erase(s.find(i+-a[x]));
s.insert(i+-a[x]+n);
}
}

Codeforces 221 E. Little Elephant and Shifts的更多相关文章

  1. Codeforces 221 D. Little Elephant and Array

    D. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

  2. Codeforces 221 C. Little Elephant and Problem

    C. Little Elephant and Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  3. Codeforces 221 B. Little Elephant and Numbers

    B. Little Elephant and Numbers time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  4. Codeforces 221 A. Little Elephant and Function

    A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...

  5. Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset

    C. Little Elephant and Shifts Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/pro ...

  6. AC日记——Little Elephant and Shifts codeforces 221e

    E - Little Elephant and Shifts 思路: 一次函数线段树(疯狂debug): b不断循环左移,判断每次最小的|i-j|,a[i]=b[j]: 仔细观察发现,每个bi移动时, ...

  7. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  8. CF--思维练习--CodeForces - 220C Little Elephant and Shifts (STL模拟)

    ACM思维题训练集合 The Little Elephant has two permutations a and b of length n, consisting of numbers from ...

  9. 【Codeforces 204E】Little Elephant and Strings

    Codeforces 204 E 题意:给\(n\)个串,求对于每一个串在至少\(k\)个串中出现的它的子串\(S_{l..r}\)有多少个. 思路:后缀自动机上\(dp\)... 我们首先构造出这\ ...

随机推荐

  1. YQCB冲刺周第二天

    YQCB冲刺周第二天 1.实现用户记账的功能 2.实现用户头像的设置 3.实现个人设置的功能 遇到的问题: 记账的分类,数据库存取图片,页面跳转+超链接的使用 团队讨论的照片:             ...

  2. C语言的知识与能力的自评

    1.我希望将来上班的地方是自己所感兴趣的,正在寻找自己感兴趣的,并且正在普及IT行业的相关知识. 2.我认为学习就是一个自我成长和自我提升以及认识世界的方法,学习的作用是可以不断的提升对这个世界的认识 ...

  3. 我是IT小小鸟(读后感)

    序 1.兴趣,这本书第一个点讲兴趣,可是在中国填鸭式的教育下,有兴趣也被这种教育给泯灭了. 2.他山之石,可以攻玉.但不可照搬.这点我非常赞同作者的看法.别人东西你拿来,一定要在他的基础上进行创   ...

  4. 陈爽 软件工程导论week2.1

    软件工程导论week2.1 第一章概论问题:1.程序=算法+数据结构  软件=程序+软件工程软件工程的目标是创造足够好的软件,可以从用户满意度,可靠性,软件流程的质量,可维护性等方面判断,但是我们没有 ...

  5. JAVA之路(一)

    距离做下复习JAVA并学好JAVA的决定已经过去一周了,我买了慕课网的JAVA入门视频,在图书馆借了三本关于JAVA的书——两本是JAVA入门经典,一本是JAVA WEB开发宝典.我的计划是短时间内复 ...

  6. lintcode-427-生成括号

    427-生成括号 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果. 样例 给定 n = 3, 可生成的组合如下: "((()))", "(()( ...

  7. [mysqld_safe]centos7 mysql 安装与配置

    查资料发现是CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了. 有两种解决办法: 安装mariadb [root@a ~]#  yum install mar ...

  8. Thread的run()与start()的区别

    java的线程是通过java.lang.Thread类来实现的.VM启动时会有一个由主方法所定义的线程.可以通过创建Thread的实例来创建新的线程.每个线程都是通过某个特定Thread对象所对应的方 ...

  9. 九度-题目1203:IP地址

    http://ac.jobdu.com/problem.php?pid=1203 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1<=n<=500), ...

  10. jQuery表单验证组件BootstrapValidator

    github:https://github.com/nghuuphuoc/bootstrapvalidator 参考博客:JS组件系列——Form表单验证神器: BootstrapValidator ...