zoukankan      html  css  js  c++  java
  • Codeforces 221 E. Little Elephant and Shifts

    E. Little Elephant and Shifts
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    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 ndistinct 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

    题意:给出一个1——n的排列a,再给出一个1——n的排列b。若a[i]==b[j],则dis=|i-j|
    每次将b序列左移1位,移n-1次,序列第一个补到最后面,问初始序列以及每次左移后的序列最小的dis

    若b[j]在a[i]的左边,随着每次左移,两点间dis+1
    若b[j]在a[i]的右边,随着每次左移,两点间dis-1

    可以统计到这一次左移,一共加了多少1,减了多少1,出队入队的时候再考虑这些1,
    即延迟标记
    每次的答案,从>=0的里面找最小的,<0的里面找最大的,两者再取最小
    multiset 模拟每次把第一个挪到最后一个的过程

    解释最后一行:n-a[x]是第一个拿到最后一个的实际距离,再加i+1是延迟标记
    #include<cstdio>
    #include<set>
    #include<algorithm>
    using namespace std;
    multiset<int>s;
    multiset<int>::iterator it;
    int a[100001],ans,b[100001];
    int main()
    {
        int n,x;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) 
        {
            scanf("%d",&x);
            a[x]=i;
        }
        for(int i=1;i<=n;i++) 
        {
            scanf("%d",&b[i]);
            s.insert(i-a[b[i]]);
        }
        for(int i=0;i<n;i++)
        {
            it=s.lower_bound(i);
            ans=1e5+10;
            if(it!=s.end())  ans=min(ans,*it-i);
            if(it!=s.begin()) ans=min(ans,i-(*--it));
            printf("%d
    ",ans);
            x=b[i+1];
            s.erase(s.find(i+1-a[x]));
            s.insert(i+1-a[x]+n);
        }
    }


  • 相关阅读:
    将台湾与山西的资源进行整合,搭建晋台两地商品营销平台
    范姜锋:致力于协助台湾青年创业,融入“一带一路”建设
    不甘于平凡,他靠借钱入行做电器生意,年销售额竟突破亿元?
    研究生接手父亲的事业,当起“猪妹”每天和猪打交道
    面对找不到工作的困难,小伙选择创业开酒吧,月收入竟达到了6万
    django 直接将数据分配给前台
    Centos7下安装与卸载Jdk1.8
    从“挖光缆”到“剪网线”|蚂蚁金服异地多活的微服务体系
    推进“互联网+政务服务” 加快新型智慧城市建设
    推进“互联网+政务服务” 加快新型智慧城市建设
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6848448.html
Copyright © 2011-2022 走看看