您的位置:首页 > 其它

Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset

2015-07-03 18:05 441 查看

C. Little Elephant and Shifts

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/220/C

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?

[b]Input[/b]

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.

[b]Output[/b]

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.

[b]Sample Input[/b]

2
1 2
2 1

[b]Sample Output[/b]

1
0

HINT

[b]题意
[/b]

给你一个a数组,一个b数组

都只含1-n

俩数组的距离定义为,if(a[i]==b[j])dis=min(dis,abs(j-i))

然后对于每一个b的排列,让你输出距离

[b]题解:[/b]

用multiset模拟一下就好了

对了,如果用multiset.erase(iterator)这样是只会删除一个的

如果multiset.erase(x),x是一个number的话,这样会把等于x的都删除

[b]代码[/b]

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1050005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//**************************************************************************************

int a[maxn];
int b[maxn];
multiset<int> s;
multiset<int>::iterator it;
int ans;
int main()
{
int n=read();
for(int i=0;i<n;i++)
{
int x=read();
a[x]=i;
}
for(int i=0;i<n;i++)
{
b[i]=read();
s.insert(i-a[b[i]]);
}
for(int i=0;i<n;i++)
{
ans=inf;
it=s.lower_bound(i);
if(it!=s.end())
ans=min(ans,*it-i);
if(it!=s.begin())
ans=min(ans,i-*(--it));
printf("%d\n",ans);
it=s.find(i-a[b[i]]);
s.erase(it);
s.insert(i-a[b[i]]+n);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: