CF1234E.Special Permutations

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

  • p1(4)=[1,2,3,4]p_1(4) = [1, 2, 3, 4];
  • p2(4)=[2,1,3,4]p_2(4) = [2, 1, 3, 4];
  • p3(4)=[3,1,2,4]p_3(4) = [3, 1, 2, 4];
  • p4(4)=[4,1,2,3]p_4(4) = [4, 1, 2, 3].

You are given an array x1,x2,…,xmx_1, x_2, \dots, x_m (1≤xi≤n1 \le x_i \le n).

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

Let's define a function f(p)=∑i=1m−1∣pos(p,xi)−pos(p,xi+1)∣f(p) = \sum\limits_{i=1}^{m - 1} |pos(p, x_i) - pos(p, x_{i + 1})|, where ∣val∣|val| is the absolute value of valval. This function means the sum of distances between adjacent elements of xx in pp.

Your task is to calculate f(p1(n)),f(p2(n)),…,f(pn(n))f(p_1(n)), f(p_2(n)), \dots, f(p_n(n)).

我们定义 pi(n)p_i(n) 为如下排列:[i,1,2,…,i−1,i+1,…,n][i, 1, 2, \dots, i - 1, i + 1, \dots, n]。这意味着第 ii 个排列几乎是一个恒等排列(即每个元素都映射到自身),但元素 ii 被置于首位。例如:

  • p1(4)=[1,2,3,4]p_1(4) = [1, 2, 3, 4];
  • p2(4)=[2,1,3,4]p_2(4) = [2, 1, 3, 4];
  • p3(4)=[3,1,2,4]p_3(4) = [3, 1, 2, 4];
  • p4(4)=[4,1,2,3]p_4(4) = [4, 1, 2, 3]。

给定一个数组 x1,x2,…,xmx_1, x_2, \dots, x_m(其中 1≤xi≤n1 \le x_i \le n)。

令 pos(p,val)pos(p, val) 表示元素 valval 在排列 pp 中的位置。因此,pos(p1(4),3)=3pos(p_1(4), 3) = 3,pos(p2(4),2)=1pos(p_2(4), 2) = 1,pos(p4(4),4)=1pos(p_4(4), 4) = 1。

我们定义函数 f(p)=∑i=1m−1∣pos(p,xi)−pos(p,xi+1)∣f(p) = \sum\limits_{i=1}^{m - 1} |pos(p, x_i) - pos(p, x_{i + 1})|,其中 ∣val∣|val| 表示 valval 的绝对值。该函数表示数组 xx 中相邻元素在排列 pp 中位置的距离之和。

你的任务是计算 f(p1(n)),f(p2(n)),…,f(pn(n))f(p_1(n)), f(p_2(n)), \dots, f(p_n(n))。

输入格式

The first line of the input contains two integers nn and mm (2≤n,m≤2⋅1052 \le n, m \le 2 \cdot 10^5) — the number of elements in each permutation and the number of elements in xx.

The second line of the input contains mm integers (mm, not nn) x1,x2,…,xmx_1, x_2, \dots, x_m (1≤xi≤n1 \le x_i \le n), where xix_i is the ii-th element of xx. Elements of xx can repeat and appear in arbitrary order.

输入的第一行包含两个整数 nn 和 mm(2≤n,m≤2⋅1052 \le n, m \le 2 \cdot 10^5)—— 分别表示每个排列中的元素个数以及数组 xx 中的元素个数。

输入的第二行包含 mm 个整数(注意是 mm 个,而非 nn 个)x1,x2,…,xmx_1, x_2, \dots, x_m(1≤xi≤n1 \le x_i \le n),其中 xix_i 是 xx 的第 ii 个元素。xx 中的元素可以重复,且可以以任意顺序出现。

输出格式

Print nn integers: f(p1(n)),f(p2(n)),…,f(pn(n))f(p_1(n)), f(p_2(n)), \dots, f(p_n(n)).

输出 nn 个整数:f(p1(n)),f(p2(n)),…,f(pn(n))f(p_1(n)), f(p_2(n)), \dots, f(p_n(n))。

输入输出样例

  • 输入#1

    4 4
    1 2 3 4

    输出#1

    3 4 6 5
  • 输入#2

    5 5
    2 1 5 3 5

    输出#2

    9 8 12 6 8
  • 输入#3

    2 10
    1 2 1 1 2 2 2 2 2 2

    输出#3

    3 3

说明/提示

Consider the first example:

x=[1,2,3,4]x = [1, 2, 3, 4], so

  • for the permutation p1(4)=[1,2,3,4]p_1(4) = [1, 2, 3, 4] the answer is ∣1−2∣+∣2−3∣+∣3−4∣=3|1 - 2| + |2 - 3| + |3 - 4| = 3;
  • for the permutation p2(4)=[2,1,3,4]p_2(4) = [2, 1, 3, 4] the answer is ∣2−1∣+∣1−3∣+∣3−4∣=4|2 - 1| + |1 - 3| + |3 - 4| = 4;
  • for the permutation p3(4)=[3,1,2,4]p_3(4) = [3, 1, 2, 4] the answer is ∣2−3∣+∣3−1∣+∣1−4∣=6|2 - 3| + |3 - 1| + |1 - 4| = 6;
  • for the permutation p4(4)=[4,1,2,3]p_4(4) = [4, 1, 2, 3] the answer is ∣2−3∣+∣3−4∣+∣4−1∣=5|2 - 3| + |3 - 4| + |4 - 1| = 5.

Consider the second example:

x=[2,1,5,3,5]x = [2, 1, 5, 3, 5], so

  • for the permutation p1(5)=[1,2,3,4,5]p_1(5) = [1, 2, 3, 4, 5] the answer is ∣2−1∣+∣1−5∣+∣5−3∣+∣3−5∣=9|2 - 1| + |1 - 5| + |5 - 3| + |3 - 5| = 9;
  • for the permutation p2(5)=[2,1,3,4,5]p_2(5) = [2, 1, 3, 4, 5] the answer is ∣1−2∣+∣2−5∣+∣5−3∣+∣3−5∣=8|1 - 2| + |2 - 5| + |5 - 3| + |3 - 5| = 8;
  • for the permutation p3(5)=[3,1,2,4,5]p_3(5) = [3, 1, 2, 4, 5] the answer is ∣3−2∣+∣2−5∣+∣5−1∣+∣1−5∣=12|3 - 2| + |2 - 5| + |5 - 1| + |1 - 5| = 12;
  • for the permutation p4(5)=[4,1,2,3,5]p_4(5) = [4, 1, 2, 3, 5] the answer is ∣3−2∣+∣2−5∣+∣5−4∣+∣4−5∣=6|3 - 2| + |2 - 5| + |5 - 4| + |4 - 5| = 6;
  • for the permutation p5(5)=[5,1,2,3,4]p_5(5) = [5, 1, 2, 3, 4] the answer is ∣3−2∣+∣2−1∣+∣1−4∣+∣4−1∣=8|3 - 2| + |2 - 1| + |1 - 4| + |4 - 1| = 8.

考虑第一个例子:

x=[1,2,3,4]x = [1, 2, 3, 4],因此

  • 对于排列 p1(4)=[1,2,3,4]p_1(4) = [1, 2, 3, 4],答案为 ∣1−2∣+∣2−3∣+∣3−4∣=3|1 - 2| + |2 - 3| + |3 - 4| = 3;
  • 对于排列 p2(4)=[2,1,3,4]p_2(4) = [2, 1, 3, 4],答案为 ∣2−1∣+∣1−3∣+∣3−4∣=4|2 - 1| + |1 - 3| + |3 - 4| = 4;
  • 对于排列 p3(4)=[3,1,2,4]p_3(4) = [3, 1, 2, 4],答案为 ∣2−3∣+∣3−1∣+∣1−4∣=6|2 - 3| + |3 - 1| + |1 - 4| = 6;
  • 对于排列 p4(4)=[4,1,2,3]p_4(4) = [4, 1, 2, 3],答案为 ∣2−3∣+∣3−4∣+∣4−1∣=5|2 - 3| + |3 - 4| + |4 - 1| = 5。

考虑第二个例子:

x=[2,1,5,3,5]x = [2, 1, 5, 3, 5],因此

  • 对于排列 p1(5)=[1,2,3,4,5]p_1(5) = [1, 2, 3, 4, 5],答案为 ∣2−1∣+∣1−5∣+∣5−3∣+∣3−5∣=9|2 - 1| + |1 - 5| + |5 - 3| + |3 - 5| = 9;
  • 对于排列 p2(5)=[2,1,3,4,5]p_2(5) = [2, 1, 3, 4, 5],答案为 ∣1−2∣+∣2−5∣+∣5−3∣+∣3−5∣=8|1 - 2| + |2 - 5| + |5 - 3| + |3 - 5| = 8;
  • 对于排列 p3(5)=[3,1,2,4,5]p_3(5) = [3, 1, 2, 4, 5],答案为 ∣3−2∣+∣2−5∣+∣5−1∣+∣1−5∣=12|3 - 2| + |2 - 5| + |5 - 1| + |1 - 5| = 12;
  • 对于排列 p4(5)=[4,1,2,3,5]p_4(5) = [4, 1, 2, 3, 5],答案为 ∣3−2∣+∣2−5∣+∣5−4∣+∣4−5∣=6|3 - 2| + |2 - 5| + |5 - 4| + |4 - 5| = 6;
  • 对于排列 p5(5)=[5,1,2,3,4]p_5(5) = [5, 1, 2, 3, 4],答案为 ∣3−2∣+∣2−1∣+∣1−4∣+∣4−1∣=8|3 - 2| + |2 - 1| + |1 - 4| + |4 - 1| = 8。

输入解题思路,AI测评打分。不知道怎么写?

首页