Codeforces Round 1112 (Div. 1)
C
双裂口转盘
Double-Rift Dial
Codeforces
2000
原题
← BD →

双裂口转盘AI (CI翻译)

A permutation$^{\text{∗}}$ $p$ of length $n$ is written clockwise on a circular dial.

长度为 $n$ 的排列$^{\text{∗}}$ $p$ 按顺时针方向写在一个圆形转盘上。AI (CI翻译)

Choose a starting position $s$ ($1\le s\le n$) and read one full circle clockwise from $p_s$, wrapping around after $p_n$. The resulting sequence is

选择一个起始位置 $s$($1\le s\le n$),从 $p_s$ 开始沿顺时针方向读取一整圈,在 $p_n$ 之后回到开头。得到的序列为AI (CI翻译)

$$ q=[p_s,p_{s+1},\ldots,p_n,p_1,\ldots,p_{s-1}], $$

$$ q=[p_s,p_{s+1},\ldots,p_n,p_1,\ldots,p_{s-1}], $$AI (CI翻译)

which also has a length of $n$.

其长度同样为 $n$。AI (CI翻译)

For any non-empty prefix of this sequence $[q_1, q_2, \ldots, q_k]$ ($k\ge 1$), let $S$ be the corresponding set of values, that is, $S=\{q_1, q_2,\ldots, q_k\}$. Split $S$ into maximal segments of consecutive integers, and we call these segments the blocks of $S$.

对于该序列的任意非空前缀 $[q_1, q_2, \ldots, q_k]$($k\ge 1$),令 $S$ 为对应的数值集合,即 $S=\{q_1, q_2,\ldots, q_k\}$。将 $S$ 划分为若干个极大的连续整数段,并称这些连续整数段为 $S$ 的块。AI (CI翻译)

For example, $S=\{1,2,5,7,8,9\}$ has $3$ blocks: $\{1,2\}$, $\{5\}$, and $\{7,8,9\}$.

例如,$S=\{1,2,5,7,8,9\}$ 有 $3$ 个块:$\{1,2\}$、$\{5\}$ 和 $\{7,8,9\}$。AI (CI翻译)

A starting position $s$ is called good if and only if, for every non-empty prefix of $q$, the corresponding set $S$ has at most $2$ blocks.

当且仅当对于 $q$ 的每个非空前缀,其对应的集合 $S$ 至多包含 $2$ 个块时,起始位置 $s$ 被称为优秀。AI (CI翻译)

Find the number of good starting positions.

求优秀起始位置的数量。AI (CI翻译)

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

$^{\text{∗}}$长度为 $n$ 的排列是一个由 $1$ 到 $n$ 中的 $n$ 个互不相同的整数按任意顺序组成的数组。例如,$[2,3,1,5,4]$ 是一个排列,但 $[1,2,2]$ 不是排列(数组中 $2$ 出现了两次),$[1,3,4]$ 也不是排列($n=3$,但数组中出现了 $4$)。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多个测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各个测试用例的描述。AI (CI翻译)

The first line of each test case contains one integer $n$ ($1\le n\le 2\cdot 10^5$) — the length of $p$.

每个测试用例的第一行包含一个整数 $n$($1\le n\le 2\cdot 10^5$),表示 $p$ 的长度。AI (CI翻译)

The second line of each test case contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$-s are distinct) — the elements of $p$.

每个测试用例的第二行包含 $n$ 个整数 $p_1,p_2,\ldots,p_n$($1\le p_i\le n$,所有 $p_i$ 互不相同),表示 $p$ 的元素。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2\cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, print one integer — the number of good starting positions.

对于每个测试用例,输出一个整数,即优秀起始位置的数量。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, there is only one starting position. Every non-empty prefix contains the single value $1$, so it has one block. Thus, this position is good.

在第一个测试用例中,只有一个起始位置。每个非空前缀都只包含值 $1$,因此有一个块。所以该位置是优秀的。AI (CI翻译)

In the second test case, after reading the first $3$ numbers from position $1$, the set is $\{1,3,5\}$ and has $3$ blocks. Thus, position $1$ is not good. Each of the other $4$ positions is good.

在第二个测试用例中,从位置 $1$ 开始读取前 $3$ 个数后,集合为 $\{1,3,5\}$,包含 $3$ 个块。因此,位置 $1$ 不是优秀的。其他 $4$ 个位置都是优秀的。AI (CI翻译)

样例

Input #1
4
1
1
5
1 3 5 2 4
6
1 2 4 5 3 6
7
1 3 5 7 2 4 6
Output #1
1
4
3
0
InputCopy OutputCopy
题目信息
题号
2249C
时间限制
2 s
内存限制
256 MB
标签
数据结构
DFS 及类似
并查集
模拟
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。