#abc268e. E - Chinese Restaurant (Three-Star Version)

E - Chinese Restaurant (Three-Star Version)

Score : 500500 points

问题描述

设有0号、1号、\ldots以及(N-1)号人士,他们按照逆时针方向均匀地围绕在一个转盘周围。第pip_i道菜位于第i号人士面前的桌子上。

你可以执行以下操作零次或多次:

  • 将转盘逆时针旋转 1N\frac{1}{N} 圈。在旋转前位于第i号人士面前的那道菜现在会出现在第 (i+1)modN(i+1) \bmod N 号人士面前。

完成所有操作后,第i号人士会增加 kk 点沮丧值,其中kk是最小整数,使得第i号菜此时位于第 (ik)modN(i-k) \bmod N 号或第 (i+k)modN(i+k) \bmod N 号人士面前。

求最终这N位人士沮丧值之和的最小可能值。

什么是amodma \bmod m?对于一个整数aa和一个正整数mmamodma \bmod m表示的是在00(m1)(m-1)(包含两端点)之间的一个整数xx,满足(ax)(a-x)mm的倍数。(可以证明这样的xx是唯一的。)

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Person 00, Person 11, \ldots, and Person (N1)(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)modN(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 (ik)modN(i-k) \bmod N or Person (i+k)modN(i+k) \bmod N.
Find the minimum possible sum of frustration of the NN people.

What is amodma \bmod m? For an integer aa and a positive integer mm, amodma \bmod m denotes the integer xx between 00 and (m1)(m-1) (inclusive) such that (ax)(a-x) is a multiple of mm. (It can be proved that such xx is unique.)

Constraints

  • 3N2×1053 \leq N \leq 2 \times 10^5
  • 0piN10 \leq p_i \leq N-1
  • pipjp_i \neq p_j if iji \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

p0p_0 \ldots pN1p_{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 22 because:

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

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

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

update @ 2024/3/10 11:18:43