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("%d\n", ans);
po = ms.find(i - a[b[i]]);
ms.erase(po);
ms.insert(i - a[b[i]] + n);
}
}

CF--思维练习--CodeForces - 220C Little Elephant and Shifts (STL模拟)的更多相关文章

  1. CF思维联系--CodeForces - 218C E - Ice Skating (并查集)

    题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...

  2. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  3. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  4. CF思维联系–CodeForces -224C - Bracket Sequence

    ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...

  5. CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)

    ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...

  6. CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)

    ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...

  7. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

  8. CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)

    Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...

  9. Codeforces D. Little Elephant and Interval(思维找规律数位dp)

    题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...

随机推荐

  1. 使用mpvue开发小程序如何定义全局变量

    我们创建好mpvue项目之后,找到src/main.js打开在后面添加一行代码 (注意:不能在const app = new Vue(App) 之前添加) Vue.prototype.globalDa ...

  2. 使用 python 查看谁没有交作业

    话说实验报告每天都要查人数,何不用程序实现 使用 python 查看谁没有交作业 version 1.0 程序嘛,肯定是可以改进的.使用该程序的前提是实验报告文件名中包含学号信息.将以上程序放在实验报 ...

  3. 白话说编程之java线程

    线程和进程: 在说多线程之前,我们先来研究一下线程,说到线程,我们又不得不说到进程,因为很多初学者会把线程和进程分不清,搞混淆. 进程: 是操作系统系统运行的最小单元.怎么理解这句话,可以这样去对比, ...

  4. 这份Java Web必读书单,值得所有Java工程师一看!

    点击蓝色"程序员书单"关注我哟 加个"星标",每天带你读好书! 经过了10多年的发展,Java Web从开发框架到社区都已经非常成熟,而目前市面上最流行的Jav ...

  5. WordPress文章阅读量统计和显示(非插件, 刷新页面不累加)

    本文已同步到专业技术网站 www.sufaith.com, 该网站专注于前后端开发技术与经验分享, 包含Web开发.Nodejs.Python.Linux.IT资讯等板块. WordPress文章阅读 ...

  6. layoutInflater参数解析与源码分析

    关于LayoutInflater方法,无论是在listview的适配器中,还是在动态添加view的时候,都会出现它的身影,最开始我在看<第一行代码>时,不知道这个方法实际的参数到底指的是什 ...

  7. ATcoder D - Handstand 2

    题目大意: 给一个数N,在小于N的所有数中,找到(A,B)的数量,其中A的第一个数字要等于B的最后的一个数字,A的最后一个数字要等于B的第一个数字. 题解:对从1到N的所有数x,用一个二维数组保存dp ...

  8. F. Count Prime Pairs

    单点时限: 2.0 sec 内存限制: 512 MB 对于数组a,如果i≠j并且ai+aj是一个质数,那么我们就称(i,j)为质数对,计算数组中质数对的个数. 输入格式 第一行输入一个n,表示数组的长 ...

  9. Flask接口开发过程中的心得2019.10.03

    完善了一下慕课网实战中的post接口开发,得到了一些进步: 代码如下: #coding=utf-8 from flask import Flask from flask import request ...

  10. CVE-2019-1388:Windows UAC 本地提权复现

    0x01 简介 用户帐户控制(User Account Control,简写作UAC)是微软公司在其Windows Vista及更高版本操作系统中采用的一种控制机制.其原理是通知用户是否对应用程序使用 ...