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. python knn自我实践

    #得到分类数据和测试数据 import pymysql import struct from numpy import * a=['']*20 #存图像 分类数据 b=[[0]*76800]*20#存 ...

  2. jquery新版本旧版本之间的坑

    JQuery自1.6.1版本开始增加一些属性,使用时尽量使用这些新的属性,例如:selected.checked.在高版本中赋值时最好用prop,如果用attr就会出现赋值不成功的问题, 一般自定义属 ...

  3. lintcode-129-重哈希

    129-重哈希 哈希表容量的大小在一开始是不确定的.如果哈希表存储的元素太多(如超过容量的十分之一),我们应该将哈希表容量扩大一倍,并将所有的哈希值重新安排.假设你有如下一哈希表: size=3, c ...

  4. vsftpd:非常安全的ftp服务端程序

    主程序:/usr/sbin/vsftpd 主配置文件:/etc/vsftpd/vsftpd.conf CentOS 6  /etc/rc.d/init.d/vsftpd chkconfig vsftp ...

  5. HTTPD解析介绍

    配置文件全局介绍 (1)主配置文件:/etc/httpd/conf/httpd.conf 全局配置:Section 1: Global Environment 第33行 中心主机配置: Section ...

  6. Kafka Shell基本命令

    创建kafka topic bin/kafka-topics.sh --zookeeper node01:2181 --create --topic t_cdr --partitions 30 --r ...

  7. 小工具xml生成记录

    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent ...

  8. js中关于array的常用方法

    最近总结了一些关于array中的常用方法, 其中大部分的方法来自于<JavaScript框架设计>这本书, 如果有更好的方法,或者有关于string的别的常用的方法,希望大家不吝赐教. 第 ...

  9. hdu 6375 百度之星 度度熊学队列

    题目链接 Problem Description 度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣. 初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作. ①1 ...

  10. solr4.2增量索引之同步(修改,删除,新增)--转载

    原文地址:http://www.jiancool.com/article/12743229775/;jsessionid=14E9B3F1BB33399799884B5C8F15DDE1  solr增 ...