#P10843. 【MX-J2-T4】Turtle and Cycles

    ID: 12267 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>二分O2优化前缀和差分双指针 two-pointer梦熊比赛

【MX-J2-T4】Turtle and Cycles

Background

Original link: https://oier.team/problems/J2E.

Problem Description

You are given a circular permutation a0,a1,,an1a_0, a_1, \ldots, a_{n - 1} of 0n10 \sim n - 1.

In one operation, you may choose an integer i[0,n1]i \in [0, n - 1] and set aia_i to a(i1)modn+a(i+1)modnaia_{(i - 1) \bmod n} + a_{(i + 1) \bmod n} - a_i.

A position i[0,n1]i \in [0, n - 1] is good if and only if a(i1)modn<aia_{(i - 1) \bmod n} < a_i and a(i+1)modn<aia_{(i + 1) \bmod n} < a_i.

The circular sequence aa is good if and only if there exists exactly one position i[0,n1]i \in [0, n - 1] such that position ii is good.

Find the minimum number of operations needed to make aa good. It can be proven that a solution always exists.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of test cases.

For each test case:

The first line contains a positive integer nn.

The second line contains nn non-negative integers a0,a1,,an1a_0, a_1, \ldots, a_{n - 1}.

Output Format

For each test case, output one line containing one integer, the minimum number of operations.

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

0
1
5

Hint

Sample Explanation

In the first test case, the initial sequence has exactly one good position i=0i = 0, so the answer is 00.

In the second test case, you can choose i=2i = 2 to perform an operation. After the operation, the sequence becomes a=[2,3,7,4,1]a = [2, 3, 7, 4, 1]. Now the sequence has exactly one good position i=2i = 2, so the answer is 11.

Constraints

This problem uses bundled testdata and enables subtask dependencies.

Subtask ID Score nn \le n\sum n \le Special Property Dependency
11 1919 66 10410^4 None None
22 1414 1212 11
33 2727 21032 \cdot 10^3 1,21, 2
44 22 21052 \cdot 10^5 ai=ia_i = i None
55 3838 None 1,2,3,41, 2, 3, 4

For all testdata, it holds that 1T1041 \le T \le 10^4, 2n,n21052 \le n, \sum n \le 2 \cdot 10^5, 0ain10 \le a_i \le n - 1, and aa is a permutation of 0n10 \sim n - 1.

Translated by ChatGPT 5