#abc268c. C - Chinese Restaurant

C - Chinese Restaurant

Score : 300300 points

问题描述

设有00号、11号、\ldots(N1)(N-1)号共NN个人,他们按照逆时针顺序均匀地围坐在一个转盘周围。桌上碟子pip_i位于第ii号人面前。

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

  • 将转盘逆时针旋转1N\frac{1}{N}圈。这样一来,在旋转前位于第ii号人面前的碟子,现在会位于第(i+1)modN(i+1) \bmod N号人面前。

操作结束后,如果碟子ii位于第(i1)modN(i-1) \bmod N号人、第ii号人或第(i+1)modN(i+1) \bmod N号人面前,则认为第ii号人是满意的。

求最多能使多少人感到满意。

什么是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 their 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. As a result, 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 is happy if Dish ii is in front of Person (i1)modN(i-1) \bmod N, Person ii, or Person (i+1)modN(i+1) \bmod N.
Find the maximum possible number of happy 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

4

The figure below shows the table after one operation.

Here, there are four happy people:

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

There cannot be five or more happy people, so the answer is 44.

Sample Input 2

3
0 1 2

Sample Output 2

3

Sample Input 3

10
3 9 6 1 7 2 8 0 5 4

Sample Output 3

5

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