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 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 1 Output 1 0 Input 4 2 1 3 4 3 4 2 1 Output 2 1 0 1 这个题预处理一下初始的dis,然后用mutiset模拟即可
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int a[maxn],b[maxn];
multiset<int> ms;
int main()
{
int n, x;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%d", &x);
a[x] = i;
}
for (int i = 0; i < n; ++i)
{
scanf("%d", &b[i]);
ms.insert(i - a[b[i]]);
}
for (int i = 0; i < n; ++i)
{
auto po = ms.lower_bound(i);
int ans = 0X3F3F3F3F;
if (po != ms.end())
ans = min(ans, *po - i);
else if (po != ms.begin())
ans = min(ans, i - *(--po));
printf("%dn", ans);
po = ms.find(i - a[b[i]]);
ms.erase(po);
ms.insert(i - a[b[i]] + n);
}
}
- 机器学习之白话adaboost元算法
- 查看占用内存多的进程
- jboss EAP 6.2 + Message Drive Bean(MDB) 整合IBM Webshpere MQ 7.5
- 通过jenkins API去build一个job
- Django---分页器、中间件
- 启动jenkins服务错误
- 如果未来的AI拥有意识,你舍得不理它吗?
- centos下安装python3
- jboss:在standalone.xml中设置系统属性(system-properties)
- iptables
- Django-form表单
- 比较git commit 两个版本之间次数
- eclipse: workspace出错导致无法启用的解决
- 【node错误】/usr/bin/env: node: No such file or directory
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 96. 不同的二叉搜索树 II Krains 2020-09-03 树
- 410. 分割数组的最大值 Krains 2020-08-29 20:21:39 动态规划二分查找
- 字典树 Krains 2020-09-01
- redis学习(十)
- vue修改浏览器的标题title
- 天天基金网数据接口
- Centos7实现开机自启EasySwoole
- input内文字与光标的初始位置调整
- 小程序push、unshift、concat
- mac下导出chrome插件及安装
- Job 资源对象
- ReplicaSet && DaemonSet 资源对象
- 批量执行crontab指定条目的注释和解注释
- Rancher集群部署后需要做的几件事
- Ingress(Nginx)日志持久化与可视化(多图预警)