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 1Output
1
0Input
4
2 1 3 4
3 4 2 1Output
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;
}
Little Elephant and Shifts(CF-220C)相关推荐
- 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 ...
- 【解题报告】随便练练二(CF 2300)
[解题报告]随便练练二(CF 2300) A:Antimatter | CF383D 题意 思路 :DP 代码 B:Physical Education Lessons | CF915E 题意 思路一 ...
- Commentator problem(CF 2)
题目链接 题目大意: 给定三个圆,询问是否存在点满足该点与三个圆夹角均相等,若存在多组解返回夹角最大值. 圆外一点到两圆夹角均相等: 即 sina = sinb = r1 / d1 = r2 / d2 ...
- D. Make a Power of Two(cf#739DIV3)
D. Make a Power of Two 链接: link. 题意: 找出将数字转换为 2 的任意幂的最小移动次数. 题解: 先将2的xxx次幂的结果以字符串形式保存,输入字符串nnn后,因为存在 ...
- Web of Lies(CF 1548A)
这是今天在打个人赛时碰见的一道题,是一道半图论半思维的题. Web of Lies 题目大意不难理解,在这里只需要注意一些细节.在加边时,只有当cnt[min]的值为1时答案才应该减1,而不是当cnt ...
- Magic Powder - 2 (CF 670_D)
http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...
- 【解题报告】博弈专场 (CF 2000~2200)前五题
[解题报告]博弈专场 (CF 2000+)前五题 A:Fox and Card Game | CF388C 题意 思路 代码 B:Berzerk | CF786A 题意 思路 代码 C:Ithea P ...
- 运动目标跟踪(十二)--KCF跟踪及CSK,CN对比
作者的主页:http://home.isr.uc.pt/~henriques/ 可以下载到文章和代码,文章的名字叫 High-Speed Tracking with Kernelized Correl ...
- 软件设计师提纲+复习资料整理(上午题)
文章目录 软件设计师考试大纲 上午题(选择题) 一.计算机组成原理 考点:CPU结构组成 考点:原码.反码.补码定点整数范围 考点:浮点数表示 考点:RISC和CISC计算机的区别 考点:奇校验与偶校 ...
最新文章
- 作为一个java程序员,常用的linux命令(越攒越多)
- 在CentOS/Debian/Ubuntu上编译安装最新版gnu make 和GNU 'binutils' (as and ld)
- 微信/QQ 中已停止访问该网页的处理办法
- [css] 请你解释下什么是浮动和它的工作原理是什么?同时浮动会引起什么问题?
- 系统架构设计师 - 软件架构设计 - 基于架构开发方法 ABSD (Architecture-Based Soft Design)
- 将稍大文件存储到远程SQL Server服务器
- linux less 阻塞输出,linux – `less`命令显示输出所用的时间
- Android通知频道,通知点
- php utf8 正则中文表达式
- Android架构纵横谈之——软件自愈能力(转载)
- 4000汉字无一重字
- 电脑备份,电脑怎么备份系统,小白提供2种备份方法
- vim 退格键(backspace)不能用
- 决策树——预测泰坦尼克号幸存者
- Javascript带按钮的轮播广告
- 非常好理解的python re正则表达式入手
- android实现延时的方法,Android实现延时总结
- 为什么onenote一直在加载_用OneNote打造全平台的数字笔记本
- Creo模型装配总结
- 电信重组又起,对移动软件供应商与SP的影响有哪些!思考:如何从3G网络中找寻我们程序员的价值..
热门文章
- 【android】读取/res/raw目录下的文件
- 视频转为flv和图片
- 柱状图用腻了?玉玦图给你更美的数据呈现
- 工作这几年,同事对我的称呼肉眼可见的在变化着......
- 从实际业务中来,到落地业务建模中去
- Springboot2集成minidao持久层
- jeecg-framework 采用eclipse-maven运行
- Python中的_main_与_init_详解
- Makefile学习(一)[第二版]
- 正确理解Python函数是第一类对象