CF2249C.Double-Rift Dial
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A permutation∗ p of length n is written clockwise on a circular dial.
Choose a starting position s (1≤s≤n) and read one full circle clockwise from ps, wrapping around after pn. The resulting sequence is
q=\[p\_s,p\_{s+1},\\ldots,p\_n,p\_1,\\ldots,p\_{s-1}\],which also has a length of n.
For any non-empty prefix of this sequence [q1,q2,…,qk] (k≥1), let S be the corresponding set of values, that is, S=q1,q2,…,qk. Split S into maximal segments of consecutive integers, and we call these segments the blocks of S.
For example, S=1,2,5,7,8,9 has 3 blocks: 1,2, 5, and 7,8,9.
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.
Find the number of good starting positions.
∗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).
一个长度为 n 的排列∗ p 按顺时针方向写在一个圆形表盘上。
选择一个起始位置 s(1≤s≤n),并从 ps 开始沿顺时针方向完整读取一圈,即在 pn 之后循环回到 p1。所得序列为
q=\[p\_s,p\_{s+1},\\ldots,p\_n,p\_1,\\ldots,p\_{s-1}\],其长度也为 n。
对序列 q 的任意非空前缀 [q1,q2,…,qk](k≥1),令 S 为其对应值的集合,即 S=q1,q2,…,qk。将 S 划分为若干个极大的连续整数段,这些段称为 S 的块(blocks)。
例如,S=1,2,5,7,8,9 共有 3 个块:1,2、5 和 7,8,9。
当且仅当对 q 的每一个非空前缀,其对应集合 S 至多包含 2 个块时,起始位置 s 被称为好位置(good)。
求好位置的个数。
∗ 长度为 n 的排列是指由 1 到 n 中 n 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,但 [1,2,2] 不是排列(数字 2 在数组中出现两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains one integer n (1≤n≤2⋅105) — the length of p.
The second line of each test case contains n integers p1,p2,…,pn (1≤pi≤n, all pi-s are distinct) — the elements of p.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 表示排列 p 的长度。
每个测试用例的第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n,且所有 pi 互不相同)—— 即排列 p 的元素。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, print one integer — the number of good starting positions.
对于每个测试用例,输出一个整数——即好起始位置的数量。
输入输出样例
输入#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
输出#1
1 4 3 0
说明/提示
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.
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,因此具有一个块。所以该位置是好的。
在第二个测试用例中,从位置 1 开始读取前 3 个数后,集合为 1,3,5,具有 3 个块。因此位置 1 不是好的。其余 4 个位置均为好的。
输入解题思路,AI测评打分。不知道怎么写?