Codeforces 221 E. Little Elephant and Shifts
2 seconds
256 megabytes
standard input
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?
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.
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.
2
1 2
2 1
1
0
4
2 1 3 4
3 4 2 1
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- AC日记——Little Elephant and Shifts codeforces 221e
E - Little Elephant and Shifts 思路: 一次函数线段树(疯狂debug): b不断循环左移,判断每次最小的|i-j|,a[i]=b[j]: 仔细观察发现,每个bi移动时, ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- 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 ...
- 【Codeforces 204E】Little Elephant and Strings
Codeforces 204 E 题意:给\(n\)个串,求对于每一个串在至少\(k\)个串中出现的它的子串\(S_{l..r}\)有多少个. 思路:后缀自动机上\(dp\)... 我们首先构造出这\ ...
随机推荐
- PSP1130
PSP时间图: 类型 任务 开始时间 结束时间 净时间 中断时间 日期 开会 开会 16:17 16:50 33 0 20171027 开会 开会 17:00 17:22 22 0 20171028 ...
- Python学习之路8 - 内置方法
abs(-230) #取绝对值 all([0,1,-5]) #如果参数里面的所有值都为真就返回真,否则返回假 any([0,1,-5]) #如果参数里面有一个值为真则返回真,否则返回假 ascii([ ...
- c# printDialog不显示问题
1.遇到问题:同样的代码,一个可以运行成功,另一个失败.百思不得其解情况下,监视下看每一个参数的属性是否一样,但参数太多,需要时间. 主要问题一般归结为:两个项目的属性编译设置不同,果然,一个x86正 ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- HDU 5187 zhx's contest 快速幂,快速加
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5187 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- Java多线程中的wait与notify
一.wait: 1. wait 是 object 类的方法, sleep 是 thread 类的方法. 2. 当前的正在我这个对象访问的线程 wait. 3. 当前的这个线程, 锁定在当前对象的这个线 ...
- php反射机制应用
用来获取指定的类的信息,包括类中的属性,方法,方法权限,注释等 用途:1.thinkPHP框架中的前置,后置控制器的实现 2.与debug_backtrace函数结合使用,文件调用的权限管理 使用方法 ...
- php框架中常用的设计模式
1.单例模式 //单例模式 class Demo { private static $obj; private function __construct() { } private function ...
- 【移动端debug-1】css3中box-shadow的溢出问题
今天做项目遇到一个box-shadow的溢出父容器的问题,如下面的代码中,子容器inner的box-shadow在没有任何设置的情况下是溢出父容器的. 代码: <!DOCTYPE html> ...
- 【Python】第一篇:python基础_1
本篇内容 Python介绍 安装 第一个程序(hello,world) 变量 用户输入(input) 数据类型 数据运算 if判断 break和continue的区别 while 循环 一. Pyth ...