Codeforces Round 1060 (Div. 2)
B
Make it Zigzag
Codeforces
1000
原题
← AC1 →
翻译中…

An arbitrary array of integers $b$ of length $m$ is considered awesome if for all $i$ ($1 \le i \lt m$):

翻译中…
  • if $i$ is odd then $b_i \lt b_{i + 1}$ holds.
  • if $i$ is even then $b_i \gt b_{i + 1}$ holds.
翻译中…

In other words, the following inequality is true: $b_1 \lt b_2 \gt b_3 \lt b_4 \ldots$

翻译中…

You are given an array of positive integers $a$ of length $n$. You may do either of the following operations any number of times, in any order:

翻译中…
  • operation $1$: select an integer $i$ ($1 \le i \le n$) and do: $a_i := \max(a_1,\ldots,a_i)$, that is, replace $a_i$ with the prefix max up to $i$.
  • operation $2$: select an integer $i$ ($1 \le i \le n$) and then decrease $a_i$ by $1$.
翻译中…

Determine the minimum number of times you need to do operation $2$ to make $a$ awesome. Note that you do not need to minimise the number of times you perform operation $1$.

翻译中…

Input

翻译中…

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.

翻译中…

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

翻译中…

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$).

翻译中…

The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each testcase, output the minimum cost to make $a$ awesome.

翻译中…

Note

翻译中…

In the first test case, the array is already awesome so no operations need to be done.

翻译中…

In the second test case, $a$ can be made awesome as follows:

翻译中…
  • use operation $2$ and decrease $a_1$ by $1$. $[\color{red}3, 3, 2, 1] \rightarrow [\color{red}2, 3, 2, 1]$.
  • use operation $1$ and change $a_4$ to $\max(2, 3, 2, 1) = 3$. $[2, 3, 2, \color{red}1] \rightarrow [2, 3, 2, \color{red}3]$.
翻译中…

$[2, 3, 2, 3]$ is awesome as $2 \lt 3 \gt 2 \lt 3$. It can be proven that this is the minimum number of times operation $2$ needs to be performed.

翻译中…

样例

Input #1
7
5
1 4 2 5 3
4
3 3 2 1
5
6 6 6 6 6
7
1 2 3 4 5 6 7
3
3 2 1
2
1 2
9
65 85 19 53 21 79 92 29 96
Output #1
0
1
3
6
1
0
13
InputCopy OutputCopy
题目信息
题号
2154B
时间限制
1.5 s
内存限制
256 MB
标签
构造
贪心

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