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\)... 我们首先构造出这\ ...
随机推荐
- git中的重要指令
git命令 任何操作都需要以 git 命令为开头 本地操作: git init 初始化一个本地仓库 新建为 master主分支 git status 查看当前分支状态 git add <文件名& ...
- python apply()函数
python apply函数的具体的含义: apply(func [, args [, kwargs ]]) 函数用于当函数参数已经存在于一个元组或字典中时,间接地调用函数.args是一个包含将要提供 ...
- SDN前瞻 传统网络的缺陷
引言 在网络发展速度如此之快的今天,传统网络的架构充满了危机,主要有这四个问题: 传统网络部署管理困难. 分布式架构瓶颈出现. 流量控制难真正实现. 设备不可编程. 现在的网络厂商 种类繁多的网络厂商 ...
- C++ Primer Plus学习:第七章
C++入门第七章:函数-C++的编程模块 函数的基本知识 要使用C++函数,必须完成如下工作: 提供函数定义 提供函数原型 调用函数 库函数是已经定义和编译好的函数,可使用标准库头文件提供原型. 定义 ...
- 第6题 ZigZag转换
题目描述如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ro ...
- Java 静态代码块&构造代码块&局部代码块
/* 静态代码块. 随着类的加载而执行.而且只执行一次. 作用: 用于给类进行初始化. */ class StaticCode { static int num ; static { num = 10 ...
- 此时本机的BootLoader程序坏了,也就是说grub第一阶段坏掉了,该如何修复
方法一:直接安装grub (1)先把MBR拷贝一份 dd if=/dev/sda of=/tmp/mbr count=1 bs=512 (2)然后再破坏 dd if=/dev/zero of=/d ...
- QTcpServer实现多客户端连接
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QTcpServer实现多客户端连接 本文地址:https://www.techiel ...
- Linux的压缩/解压缩文件处理 zip & unzip
Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服 ...
- ADOQuery的ltBatchOptimistic状态下的用法
在ADO的ltBatchOptimistic状态下(即缓存状态),如何实现单条记录的删除与修改,也可以选择的删除或修改? 一样的删除,只是最后提交方式不一样,以前的提交最后加上try ADOCon ...