CF1853A.Desorting
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Call an array a of length n sorted if a1≤a2≤…≤an−1≤an.
Ntarsis has an array a of length n.
He is allowed to perform one type of operation on it (zero or more times):
- Choose an index i (1≤i≤n−1).
- Add 1 to a1,a2,…,ai.
- Subtract 1 from ai+1,ai+2,…,an.
The values of a can be negative after an operation.
Determine the minimum operations needed to make a not sorted.
称一个长度为 n 的数组 a 是已排序的,当且仅当满足 a1≤a2≤…≤an−1≤an。
Ntarsis 有一个长度为 n 的数组 a。
他可以对数组执行一种操作(可执行零次或多次):
- 选择一个下标 i(满足 1≤i≤n−1);
- 将 a1,a2,…,ai 各加 1;
- 将 ai+1,ai+2,…,an 各减 1。
一次操作后,数组 a 中的元素值可以为负数。
请确定使 a 不再有序(即不满足非递减性质)所需的最少操作次数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤500) — the length of the array a.
The next line contains n integers a1,a2,…,an (1≤ai≤109) — the values of array a.
It is guaranteed that the sum of n across all test cases does not exceed 500.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤100)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤500)——数组 a 的长度。
下一行包含 n 个整数 a1,a2,…,an(1≤ai≤109)——数组 a 的元素值。
保证所有测试用例中 n 的总和不超过 500。
输出格式
Output the minimum number of operations needed to make the array not sorted.
输出使数组变为非有序所需的最少操作次数。
输入输出样例
输入#1
4 2 1 1 4 1 8 10 13 3 1 3 2 3 1 9 14
输出#1
1 2 0 3
说明/提示
In the first case, we can perform 1 operation to make the array not sorted:
- Pick i=1. The array a then becomes [2,0], which is not sorted.
In the second case, we can perform 2 operations to make the array not sorted:
- Pick i=3. The array a then becomes [2,9,11,12].
- Pick i=3. The array a then becomes [3,10,12,11], which is not sorted.
It can be proven that 1 and 2 operations are the minimal numbers of operations in the first and second test cases, respectively.
In the third case, the array is already not sorted, so we perform 0 operations.
在第一种情况下,我们只需执行 1 次操作即可使数组变为非有序:
- 选择 i=1。此时数组 a 变为 [2,0],该数组非有序。
在第二种情况下,我们需执行 2 次操作才能使数组变为非有序:
- 选择 i=3。此时数组 a 变为 [2,9,11,12]。
- 再次选择 i=3。此时数组 a 变为 [3,10,12,11],该数组非有序。
可以证明:在第一个和第二个测试用例中,所需的最少操作次数分别为 1 和 2。
在第三种情况下,数组本身已为非有序,因此我们执行 0 次操作。
输入解题思路,AI测评打分。不知道怎么写?