zoukankan      html  css  js  c++  java
  • Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset

    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

    2
    1 2
    2 1

    Sample Output

    1
    0

    HINT

    题意

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

    都只含1-n

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

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

    题解:

    用multiset模拟一下就好了

    对了,如果用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);
        }
    }
  • 相关阅读:
    override CreateParams events in delphi
    .NET下获取网页的内容的封装类
    WAYOS三天重启硬件版PCB和程序已设计完成,如果需要的人多就发去工厂统一制作
    WAYOS策略路由专用工具——进程端口自动扫描导入工具
    打造最专业的三天重启工具,本人再对WAYOS智能重启进行全面升级
    WAYOS BCM 机器+授权,全淘宝最低价,全新机器仅258还包邮费
    BCM路由全智能固件升级软件tftp,一键刷路由及常用固件下载
    WAYOS PPPOE用户数据定时备份并上传到FTP,保证数据不会因为掉配置、挂机等而丢失
    WAYOS内置免拉黑终于突破技术大关完美成功,以后再也不需要独立的电脑来运行免拉黑了
    巧用EasyRadius计费策略设置灵的计费费率,保证帐目一目了然
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4619392.html
Copyright © 2011-2022 走看看