CF2249C.Double-Rift Dial

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A permutation∗^{\text{∗}} pp of length nn is written clockwise on a circular dial.

Choose a starting position ss (1≤s≤n1\le s\le n) and read one full circle clockwise from psp_s, wrapping around after pnp_n. 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 nn.

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

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

A starting position ss is called good if and only if, for every non-empty prefix of qq, the corresponding set SS has at most 22 blocks.

Find the number of good starting positions.

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

一个长度为 nn 的排列∗^{\text{∗}} pp 按顺时针方向写在一个圆形表盘上。

选择一个起始位置 ss(1≤s≤n1\le s\le n),并从 psp_s 开始沿顺时针方向完整读取一圈,即在 pnp_n 之后循环回到 p1p_1。所得序列为

q=\[p\_s,p\_{s+1},\\ldots,p\_n,p\_1,\\ldots,p\_{s-1}\],

其长度也为 nn。

对序列 qq 的任意非空前缀 [q1,q2,…,qk][q_1, q_2, \ldots, q_k](k≥1k\ge 1),令 SS 为其对应值的集合,即 S=q1,q2,…,qkS={q_1, q_2,\ldots, q_k}。将 SS 划分为若干个极大的连续整数段,这些段称为 SS 的块(blocks)。

例如,S=1,2,5,7,8,9S={1,2,5,7,8,9} 共有 33 个块:1,2{1,2}、5{5} 和 7,8,9{7,8,9}。

当且仅当对 qq 的每一个非空前缀,其对应集合 SS 至多包含 22 个块时,起始位置 ss 被称为好位置(good)。

求好位置的个数。

∗^{\text{∗}} 长度为 nn 的排列是指由 11 到 nn 中 nn 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

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

The first line of each test case contains one integer nn (1≤n≤2⋅1051\le n\le 2\cdot 10^5) — the length of pp.

The second line of each test case contains nn integers p1,p2,…,pnp_1,p_2,\ldots,p_n (1≤pi≤n1\le p_i\le n, all pip_i-s are distinct) — the elements of pp.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051\le n\le 2\cdot 10^5)—— 表示排列 pp 的长度。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1,p_2,\ldots,p_n(1≤pi≤n1\le p_i\le n,且所有 pip_i 互不相同)—— 即排列 pp 的元素。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot 10^5。

输出格式

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 11, so it has one block. Thus, this position is good.

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

在第一个测试用例中,只有一个起始位置。每个非空前缀都只包含单个数值 11,因此具有一个块。所以该位置是好的。

在第二个测试用例中,从位置 11 开始读取前 33 个数后,集合为 1,3,5{1,3,5},具有 33 个块。因此位置 11 不是好的。其余 44 个位置均为好的。

输入解题思路,AI测评打分。不知道怎么写?

首页