Codeforces Round 1083 (Div. 2)
D
Simons and Beating Peaks
Codeforces
1700
原题
← CE →
翻译中…

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

翻译中…

We call an array $b$ of length $m$ cool if and only if:

翻译中…
  • There exists no index $i$ ($1 \lt i \lt m$) such that $b_i=\max(\{b_{i-1}, b_i, b_{i+1}\})$.
翻译中…

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

翻译中…

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

翻译中…
  • Choose an index $i$ ($1 \lt i \lt n$) such that $a_i=\max(\{a_{i-1}, a_i, a_{i+1}\})$.
  • Then, he can remove either $a_{i-1}$ or $a_{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 $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).

翻译中…

Input

翻译中…

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

翻译中…

The first line contains an integer $n$ ($3\le n\le 5\cdot 10^5$) — the size of $a$.

翻译中…

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$, all $a_i$-s are distinct) — the array Simons has initially.

翻译中…

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

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

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

翻译中…

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

翻译中…
  • Choose index $3$, because $a_3=\max(\{a_2,a_3,a_4\})$. Then, he removes $a_{2}$ from the array. The array becomes $[4,3,2,5]$.
翻译中…

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

翻译中…

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

翻译中…
  • Choose index $2$. Then, he removes $a_1$ from the array. The array becomes $[5,3,6,2,1]$.
  • Choose index $3$. Then, he removes $a_2$ from the array. The array becomes $[5,6,2,1]$.
  • Choose index $2$. Then, he removes $a_1$ from the array. The array becomes $[6,2,1]$.
翻译中…

Thus, Simons makes the array cool in $3$ operations.

翻译中…

样例

Input #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
Output #1
0
1
3
3
9
InputCopy OutputCopy
题目信息
题号
2205D
时间限制
2 s
内存限制
512 MB
标签
数据结构
分治
动态规划
贪心
模拟
树

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