102684: [AtCoder]ABC268 E - Chinese Restaurant (Three-Star Version)

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

Person $0$, Person $1$, $\ldots$, and Person $(N-1)$ are sitting around a turntable in counterclockwise order, evenly spaced. Dish $p_i$ is in front of Person $i$ on the table.
You may perform the following operation $0$ or more times:

  • Rotate the turntable by one $N$-th of a counterclockwise turn. The dish that was in front of Person $i$ right before the rotation is now in front of Person $(i+1) \bmod N$.

When you are finished, Person $i$ gains frustration of $k$, where $k$ is the minimum integer such that Dish $i$ is in front of either Person $(i-k) \bmod N$ or Person $(i+k) \bmod N$.
Find the minimum possible sum of frustration of the $N$ people.

What is $a \bmod m$? For an integer $a$ and a positive integer $m$, $a \bmod m$ denotes the integer $x$ between $0$ and $(m-1)$ (inclusive) such that $(a-x)$ is a multiple of $m$. (It can be proved that such $x$ is unique.)

Constraints

  • $3 \leq N \leq 2 \times 10^5$
  • $0 \leq p_i \leq N-1$
  • $p_i \neq p_j$ if $i \neq j$.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$p_0$ $\ldots$ $p_{N-1}$

Output

Print the answer.


Sample Input 1

4
1 2 0 3

Sample Output 1

2

The figure below shows the table after one operation.

Here, the sum of their frustration is $2$ because:

  • Person $0$ gains a frustration of $1$ since Dish $0$ is in front of Person $3\ (=(0-1) \bmod 4)$;
  • Person $1$ gains a frustration of $0$ since Dish $1$ is in front of Person $1\ (=(1+0) \bmod 4)$;
  • Person $2$ gains a frustration of $0$ since Dish $2$ is in front of Person $2\ (=(2+0) \bmod 4)$;
  • Person $3$ gains a frustration of $1$ since Dish $3$ is in front of Person $0\ (=(3+1) \bmod 4)$.

We cannot make the sum of their frustration less than $2$, so the answer is $2$.


Sample Input 2

3
0 1 2

Sample Output 2

0

Sample Input 3

10
3 9 6 1 7 2 8 0 5 4

Sample Output 3

20

Input

题意翻译

$ n $ 个人进行圆排列,即围坐在圆桌周围,位置编号依次为 $ [0, n - 1] $,给定序列 $ P_n $ 表示第 $ i $ 个人喜欢的菜品在 $ P_i $ 处,$ P_i \in [0, n - 1] $ 且各不相同。定义每个人的沮丧值为其位置与 $ P_i $ 的距离。你可以任意旋转圆桌,以最小化所有人的沮丧值之和,求最小值。

Output

分数:500分

问题描述

Person $0$, Person $1$, $\ldots$, 和 Person $(N-1)$ 按逆时针顺序均匀地坐在一个转盘周围。桌面上,Person $i$ 前方有 Dish $p_i$。

  • 你可以进行0次或多次以下操作:
    • 逆时针转动转盘1/ $N$ 圈。旋转前在 Person $i$ 前方的盘子现在在 Person $(i+1) \bmod N$ 的前方。

完成后,Person $i$ 的挫败感增加 $k$,其中 $k$ 是最小的整数,使得 Dish $i$ 在 Person $(i-k) \bmod N$ 或 Person $(i+k) \bmod N$ 的前方。

什么是 $a \bmod m$? 对于整数 $a$ 和正整数 $m$,$a \bmod m$ 表示 0 到 $(m-1)$(含)之间的整数 $x$,使得 $(a-x)$ 是 $m$ 的倍数。 (可以证明这样的 $x$ 是唯一的。)

约束

  • $3 \leq N \leq 2 \times 10^5$
  • $0 \leq p_i \leq N-1$
  • 如果 $i \neq j$,则 $p_i \neq p_j$。
  • 输入中的所有值都是整数。

输入

输入通过标准输入给出以下格式:

$N$
$p_0$ $\ldots$ $p_{N-1}$

输出

打印答案。


样例输入 1

4
1 2 0 3

样例输出 1

2

下图显示了进行一次操作后的桌子。

在这里,他们的挫败感总和为 $2$,因为:

  • Person $0$ 的挫败感增加了 $1$,因为 Dish $0$ 在 Person $3\ (=(0-1) \bmod 4)$ 的前方;
  • Person $1$ 的挫败感增加了 $0$,因为 Dish $1$ 在 Person $1\ (=(1+0) \bmod 4)$ 的前方;
  • Person $2$ 的挫败感增加了 $0$,因为 Dish $2$ 在 Person $2\ (=(2+0) \bmod 4)$ 的前方;
  • Person $3$ 的挫败感增加了 $1$,因为 Dish $3$ 在 Person $0\ (=(3+1) \bmod 4)$ 的前方。

我们无法使他们的挫败感总和小于 $2$,所以答案是 $2$。


样例输入 2

3
0 1 2

样例输出 2

0

样例输入 3

10
3 9 6 1 7 2 8 0 5 4

样例输出 3

20

加入题单

算法标签: