#4641. Chinese Restaurant (Three-Star Version)

0

Chinese Restaurant (Three-Star Version)

Chinese Restaurant (Three-Star Version)

题目描述

Person 00, Person 11, …\ldots, and Person (N−1)(N-1) are sitting around a turntable in counterclockwise order, evenly spaced. Dish pip_i is in front of Person ii on the table.
You may perform the following operation 00 or more times:

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

When you are finished, Person ii gains frustration of kk, where kk is the minimum integer such that Dish ii is in front of either Person (i−k) mod N(i-k) \bmod N or Person (i+k) mod N(i+k) \bmod N.
Find the minimum possible sum of frustration of the NN 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.)

输入格式

Input is given from Standard Input in the following format: NN p0p_0 …\ldots pN−1p_{N-1}

输出格式

Print the answer.

4
1 2 0 3
2
3
0 1 2
0
10
3 9 6 1 7 2 8 0 5 4
20

提示

3≤N≤2×1053 \leq N \leq 2 \times 10^5 0≤pi≤N−10 \leq p_i \leq N-1 pi≠pjp_i \neq p_j if i≠ji \neq j. All values in input are integers.下面的图展示了一次操作之后的桌面情况。 在这里,他们的沮丧总和为 22,因为:

Person 00 gains a frustration of 11 since Dish 00 is in front of Person 3 (=(0−1) mod 4)3\ (=(0-1) \bmod 4); Person 11 gains a frustration of 00 since Dish 11 is in front of Person 1 (=(1+0) mod 4)1\ (=(1+0) \bmod 4); Person 22 gains a frustration of 00 since Dish 22 is in front of Person 2 (=(2+0) mod 4)2\ (=(2+0) \bmod 4); Person 33 gains a frustration of 11 since Dish 33 is in front of Person 0 (=(3+1) mod 4)0\ (=(3+1) \bmod 4).

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

标签: AtCoder|abc268E

来源

AtCoder|abc268E