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

8143 단어 codeforces
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?
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.
Sample Input
21 22 1
Sample Output
10
HINT
 
제목
a 배열 하나, b 배열 하나 드릴 게 요.
모두 1 - n 만 포함
두 배열 의 거 리 는 if (a [i] = b [j]) dis = min (dis, abs (j - i) 로 정의 합 니 다.
그리고 모든 b 의 배열 에 대해 출력 거 리 를 줍 니 다.
문제 풀이:
멀 티 셋 으로 시 뮬 레이 션 해 주시 면 됩 니 다.
참, multiset. erase (iterator) 를 사용 하면 하나만 삭 제 됩 니 다.
multiset. erase (x), x 가 number 라면 x 와 같은 것 을 모두 삭제 합 니 다.
코드
 
#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
",ans); it=s.find(i-a[b[i]]); s.erase(it); s.insert(i-a[b[i]]+n); } }

좋은 웹페이지 즐겨찾기