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);
        }
    }
  • 相关阅读:
    libsvm参数学习和核函数使用(转载)
    libsvm 训练后的模型参数讲解(转)
    Ternary Search Trees 三分搜索树
    分支界定法 branch-and-bound 分析与实现)(转载)
    几种常见的激活函数(转载)
    matlab神经网络实验
    递归神经网络2(转载)
    线性代数和numpy——黑板客老师课程学习
    关于 Intellij IDEA Ultimate Edition 14.1控制台中文乱码 解决
    Intellij IDEA Ultimate Edition 14.1 破解
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4619392.html
Copyright © 2011-2022 走看看