CF1853A.Desorting

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Call an array aa of length nn sorted if a1≤a2≤…≤an−1≤ana_1 \leq a_2 \leq \ldots \leq a_{n-1} \leq a_n.

Ntarsis has an array aa of length nn.

He is allowed to perform one type of operation on it (zero or more times):

  • Choose an index ii (1≤i≤n−11 \leq i \leq n-1).
  • Add 11 to a1,a2,…,aia_1, a_2, \ldots, a_i.
  • Subtract 11 from ai+1,ai+2,…,ana_{i+1}, a_{i+2}, \ldots, a_n.

The values of aa can be negative after an operation.

Determine the minimum operations needed to make aa not sorted.

称一个长度为 nn 的数组 aa 是已排序的,当且仅当满足 a1≤a2≤…≤an−1≤ana_1 \leq a_2 \leq \ldots \leq a_{n-1} \leq a_n。

Ntarsis 有一个长度为 nn 的数组 aa。

他可以对数组执行一种操作(可执行零次或多次):

  • 选择一个下标 ii(满足 1≤i≤n−11 \leq i \leq n-1);
  • 将 a1,a2,…,aia_1, a_2, \ldots, a_i 各加 11;
  • 将 ai+1,ai+2,…,ana_{i+1}, a_{i+2}, \ldots, a_n 各减 11。

一次操作后,数组 aa 中的元素值可以为负数。

请确定使 aa 不再有序(即不满足非递减性质)所需的最少操作次数。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001 \le t \le 100). The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤5002 \leq n \leq 500) — the length of the array aa.

The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \leq a_i \leq 10^9) — the values of array aa.

It is guaranteed that the sum of nn across all test cases does not exceed 500500.

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

每个测试用例的第一行包含一个整数 nn(2≤n≤5002 \leq n \leq 500)——数组 aa 的长度。

下一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \leq a_i \leq 10^9)——数组 aa 的元素值。

保证所有测试用例中 nn 的总和不超过 500500。

输出格式

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 11 operation to make the array not sorted:

  • Pick i=1i = 1. The array aa then becomes [2,0][2, 0], which is not sorted.

In the second case, we can perform 22 operations to make the array not sorted:

  • Pick i=3i = 3. The array aa then becomes [2,9,11,12][2, 9, 11, 12].
  • Pick i=3i = 3. The array aa then becomes [3,10,12,11][3, 10, 12, 11], which is not sorted.

It can be proven that 11 and 22 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 00 operations.

在第一种情况下,我们只需执行 11 次操作即可使数组变为非有序:

  • 选择 i=1i = 1。此时数组 aa 变为 [2,0][2, 0],该数组非有序。

在第二种情况下,我们需执行 22 次操作才能使数组变为非有序:

  • 选择 i=3i = 3。此时数组 aa 变为 [2,9,11,12][2, 9, 11, 12]。
  • 再次选择 i=3i = 3。此时数组 aa 变为 [3,10,12,11][3, 10, 12, 11],该数组非有序。

可以证明:在第一个和第二个测试用例中,所需的最少操作次数分别为 11 和 22。

在第三种情况下,数组本身已为非有序,因此我们执行 00 次操作。

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

首页