CF2205D.Simons and Beating Peaks

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

My loveliest wishes dissolved into the air; At eighteen, I poured my dreams into the mic to share.

— SHUN, 720

We call an array bb of length mm cool if and only if:

  • There exists no index ii (1<i<m1 \lt i \lt m) such that bi=max⁡(bi−1,bi,bi+1)b_i=\max({b_{i-1}, b_i, b_{i+1}}).

Simons has an array aa of size nn. Initially, the array is a permutation∗^{\text{∗}}.

He must perform the following operation until the array is cool:

  • Choose an index ii (1<i<n1 \lt i \lt n) such that ai=max⁡(ai−1,ai,ai+1)a_i=\max({a_{i-1}, a_i, a_{i+1}}).
  • Then, he can remove either ai−1a_{i-1} or ai+1a_{i+1} from the array. After removal, a gap appears in the array, and the left and right sides of the gap will be rejoined.

Find the minimum number of operations for Simons to perform.

∗^{\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).

我最美好的愿望消散在空气中;十八岁那年,我将梦想倾注于麦克风中与人分享。

— SHUN,720

我们称一个长度为 mm 的数组 bb 是“酷”的,当且仅当:

  • 不存在下标 ii(满足 1<i<m1 \lt i \lt m),使得 bi=max⁡(bi−1,bi,bi+1)b_i=\max({b_{i-1}, b_i, b_{i+1}})。

西蒙斯有一个长度为 nn 的数组 aa。初始时,该数组是一个排列∗^{\text{∗}}。

他必须重复执行以下操作,直到数组变为“酷”的为止:

  • 选择一个下标 ii(满足 1<i<n1 \lt i \lt n),使得 ai=max⁡(ai−1,ai,ai+1)a_i=\max({a_{i-1}, a_i, a_{i+1}});
  • 然后,他可以从数组中删除 ai−1a_{i-1} 或 ai+1a_{i+1} 中的任意一个元素。删除后,数组中会出现一个空缺,空缺左右两侧的元素将重新连接。

求西蒙斯需要执行的最少操作次数。

∗^{\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≤5⋅1041 \le t \le 5\cdot 10^4). The description of the test cases follows.

The first line contains an integer nn (3≤n≤5⋅1053\le n\le 5\cdot 10^5) — the size of aa.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤n1\le a_i\le n, all aia_i-s are distinct) — the array Simons has initially.

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

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

第一行包含一个整数 nn(3≤n≤5⋅1053\le n\le 5\cdot 10^5)—— 数组 aa 的大小。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤n1\le a_i\le n,且所有 aia_i 互不相同)—— Simons 初始拥有的数组。

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

输出格式

For each test case, print a single integer — the minimum number of operations for Simons to perform.

对于每个测试用例,输出一个整数——西蒙需要执行的最少操作次数。

输入输出样例

  • 输入#1

    5
    3
    1 2 3
    5
    4 1 3 2 5
    6
    4 5 3 6 2 1
    7
    6 5 1 7 4 2 3
    15
    7 4 10 12 9 14 5 3 8 11 1 15 2 13 6

    输出#1

    0
    1
    3
    3
    9

说明/提示

In the first test case, the array is cool initially, so Simons can't perform any operation. The answer is 00.

In the second test case, Simons can perform as follows:

  • Choose index 33, because a3=max⁡(a2,a3,a4)a_3=\max({a_2,a_3,a_4}). Then, he removes a2a_{2} from the array. The array becomes [4,3,2,5][4,3,2,5].

We can see the array is cool now. Thus, the answer is 11.

In the third test case, Simons can perform as follows:

  • Choose index 22. Then, he removes a1a_1 from the array. The array becomes [5,3,6,2,1][5,3,6,2,1].
  • Choose index 33. Then, he removes a2a_2 from the array. The array becomes [5,6,2,1][5,6,2,1].
  • Choose index 22. Then, he removes a1a_1 from the array. The array becomes [6,2,1][6,2,1].

Thus, Simons makes the array cool in 33 operations.

在第一个测试用例中,数组初始时就是“酷”的,因此 Simon 无法执行任何操作。答案为 00。

在第二个测试用例中,Simon 可以按如下方式操作:

  • 选择下标 33,因为 a3=max⁡(a2,a3,a4)a_3=\max({a_2,a_3,a_4})。然后他将 a2a_{2} 从数组中删除。数组变为 [4,3,2,5][4,3,2,5]。

此时可见数组已变为“酷”的。因此答案为 11。

在第三个测试用例中,Simon 可以按如下方式操作:

  • 选择下标 22。然后他将 a1a_1 从数组中删除。数组变为 [5,3,6,2,1][5,3,6,2,1]。
  • 选择下标 33。然后他将 a2a_2 从数组中删除。数组变为 [5,6,2,1][5,6,2,1]。
  • 选择下标 22。然后他将 a1a_1 从数组中删除。数组变为 [6,2,1][6,2,1]。

因此,Simon 经过 33 次操作使数组变为“酷”的。

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

首页