CF2154B.Make it Zigzag

入门

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

An arbitrary array of integers bb of length mm is considered awesome if for all ii (1≤i<m1 \le i \lt m):

  • if ii is odd then bi<bi+1b_i \lt b_{i + 1} holds.
  • if ii is even then bi>bi+1b_i \gt b_{i + 1} holds.

In other words, the following inequality is true: b1<b2>b3<b4…b_1 \lt b_2 \gt b_3 \lt b_4 \ldots

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

  • operation 11: select an integer ii (1≤i≤n1 \le i \le n) and do: ai:=max⁡(a1,…,ai)a_i := \max(a_1,\ldots,a_i), that is, replace aia_i with the prefix max up to ii.
  • operation 22: select an integer ii (1≤i≤n1 \le i \le n) and then decrease aia_i by 11.

Determine the minimum number of times you need to do operation 22 to make aa awesome. Note that you do not need to minimise the number of times you perform operation 11.

一个长度为 mm 的任意整数数组 bb 被称为“极好”(awesome),当且仅当对所有 ii(1≤i<m1 \le i \lt m)满足:

  • 若 ii 为奇数,则 bi<bi+1b_i \lt b_{i + 1};
  • 若 ii 为偶数,则 bi>bi+1b_i \gt b_{i + 1}。

换言之,以下不等式成立:b1<b2>b3<b4…b_1 \lt b_2 \gt b_3 \lt b_4 \ldots

现给定一个长度为 nn 的正整数数组 aa。你可以以任意顺序、任意次数执行以下两种操作之一:

  • 操作 11:选择一个整数 ii(1≤i≤n1 \le i \le n),并令 ai:=max⁡(a1,…,ai)a_i := \max(a_1,\ldots,a_i),即用前缀最大值(从 a1a_1 到 aia_i 的最大值)替换 aia_i;
  • 操作 22:选择一个整数 ii(1≤i≤n1 \le i \le n),然后将 aia_i 减 11。

请确定为使 aa 变为“极好”数组所需执行操作 22 的最小次数。注意:你无需最小化操作 11 的执行次数。

输入格式

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 an integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤1091 \le a_i \le 10^9).

The sum of nn across all test cases does not exceed 2⋅1052 \cdot 10^5.

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

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

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤1091 \le a_i \le 10^9)。

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

输出格式

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

对于每个测试用例,输出使 aa 变为“卓越数”(awesome)的最小代价。

输入输出样例

  • 输入#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

    输出#1

    0
    1
    3
    6
    1
    0
    13

说明/提示

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

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

  • use operation 22 and decrease a1a_1 by 11. [3,3,2,1]→[2,3,2,1][\color{red}3, 3, 2, 1] \rightarrow [\color{red}2, 3, 2, 1].
  • use operation 11 and change a4a_4 to max⁡(2,3,2,1)=3\max(2, 3, 2, 1) = 3. [2,3,2,1]→[2,3,2,3][2, 3, 2, \color{red}1] \rightarrow [2, 3, 2, \color{red}3].

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

在第一个测试用例中,数组已经是“awesome”的,因此无需执行任何操作。

在第二个测试用例中,可通过以下方式将 aa 变为“awesome”:

  • 使用操作 22,将 a1a_1 减少 11:[3,3,2,1]→[2,3,2,1][\color{red}3, 3, 2, 1] \rightarrow [\color{red}2, 3, 2, 1]。
  • 使用操作 11,将 a4a_4 修改为 max⁡(2,3,2,1)=3\max(2, 3, 2, 1) = 3:[2,3,2,1]→[2,3,2,3][2, 3, 2, \color{red}1] \rightarrow [2, 3, 2, \color{red}3]。

[2,3,2,3][2, 3, 2, 3] 是“awesome”的,因为满足 2<3>2<32 \lt 3 \gt 2 \lt 3。可以证明,这是所需执行操作 22 的最少次数。

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

首页