Codeforces Round #590 (Div. 3) E. Special Permutations

link:

https://codeforces.com/contest/1234/problem/E

Meaning of the questions:

Let's define pi(n) as the following permutation: [i,1,2,…,i−1,i+1,…,n]. This means that the i-th permutation is almost identity (i.e. which maps every element to itself) permutation but the element i is on the first position. Examples:

p1(4)=[1,2,3,4];
p2(4)=[2,1,3,4];
p3(4)=[3,1,2,4];
p4(4)=[4,1,2,3].
You are given an array x1,x2,…,xm (1≤xi≤n).

Let pos(p,val) be the position of the element val in p. So, pos(p1(4),3)=3,pos(p2(4),2)=1,pos(p4(4),4)=1.

Let's define a function f(p)=∑i=1m−1|pos(p,xi)−pos(p,xi+1)|, where |val| is the absolute value of val. This function means the sum of distances between adjacent elements of x in p.

Your task is to calculate f(p1(n)),f(p2(n)),…,f(pn(n)).

Ideas:

Consideration of each adjacent two values across all points on the p, when moved to the front, and is decremented by one.
First calculated, each value is shifted due to impact previously.
Rerecording values for each two adjacent, one by one calculation.

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
int a[MAXN], cnt[MAXN];
LL res[MAXN];
vector<int> vec[MAXN];
int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1;i <= m;i++)
        scanf("%d", &a[i]);
    for (int i = 1;i < m;i++)
        res[1] += abs(a[i]-a[i+1]);
    for (int i = 1;i < m;i++)
    {
        int l = a[i], r = a[i + 1];
        if (l == r)
            continue;
        vec[l].push_back(r);
        vec[r].push_back(l);
        if (l > r)
            swap(l, r);
        if (r - l < 2)
            continue;
        cnt[l + 1]++;
        cnt[r]--;
    }
    for (int i = 1;i < n;i++)
        cnt[i+1] += cnt[i];
    for (int i = 2;i <= n;i++)
    {
        res[i] = res[1]-cnt[i];
        for (auto x: vec[i])
        {
            res[i] -= abs(i-x);
            res[i] += x-1+(x<i);
        }
    }
    for (int i = 1;i <= n;i++)
        printf("%I64d ", res[i]);

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11619532.html