CF2154B.Make it Zigzag
入门
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
An arbitrary array of integers b of length m is considered awesome if for all i (1≤i<m):
- if i is odd then bi<bi+1 holds.
- if i is even then bi>bi+1 holds.
In other words, the following inequality is true: b1<b2>b3<b4…
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≤i≤n) and do: ai:=max(a1,…,ai), that is, replace ai with the prefix max up to i.
- operation 2: select an integer i (1≤i≤n) and then decrease ai 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.
一个长度为 m 的任意整数数组 b 被称为“极好”(awesome),当且仅当对所有 i(1≤i<m)满足:
- 若 i 为奇数,则 bi<bi+1;
- 若 i 为偶数,则 bi>bi+1。
换言之,以下不等式成立:b1<b2>b3<b4…
现给定一个长度为 n 的正整数数组 a。你可以以任意顺序、任意次数执行以下两种操作之一:
- 操作 1:选择一个整数 i(1≤i≤n),并令 ai:=max(a1,…,ai),即用前缀最大值(从 a1 到 ai 的最大值)替换 ai;
- 操作 2:选择一个整数 i(1≤i≤n),然后将 ai 减 1。
请确定为使 a 变为“极好”数组所需执行操作 2 的最小次数。注意:你无需最小化操作 1 的执行次数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains an integer n (2≤n≤2⋅105) — the length of the array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109).
The sum of n across all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)—— 表示数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)。
所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each testcase, output the minimum cost to make a awesome.
对于每个测试用例,输出使 a 变为“卓越数”(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, a can be made awesome as follows:
- use operation 2 and decrease a1 by 1. [3,3,2,1]→[2,3,2,1].
- use operation 1 and change a4 to max(2,3,2,1)=3. [2,3,2,1]→[2,3,2,3].
[2,3,2,3] is awesome as 2<3>2<3. It can be proven that this is the minimum number of times operation 2 needs to be performed.
在第一个测试用例中,数组已经是“awesome”的,因此无需执行任何操作。
在第二个测试用例中,可通过以下方式将 a 变为“awesome”:
- 使用操作 2,将 a1 减少 1:[3,3,2,1]→[2,3,2,1]。
- 使用操作 1,将 a4 修改为 max(2,3,2,1)=3:[2,3,2,1]→[2,3,2,3]。
[2,3,2,3] 是“awesome”的,因为满足 2<3>2<3。可以证明,这是所需执行操作 2 的最少次数。
输入解题思路,AI测评打分。不知道怎么写?