CF1898B.Milena and Admirer

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Milena has received an array of integers a1,a2,…,ana_1, a_2, \ldots, a_n of length nn from a secret admirer. She thinks that making it non-decreasing should help her identify the secret admirer.

She can use the following operation to make this array non-decreasing:

  • Select an element aia_i of array aa and an integer xx such that 1≤x<ai1 \le x \lt a_i. Then, replace aia_i by two elements xx and ai−xa_i - x in array aa. New elements (xx and ai−xa_i - x) are placed in the array aa in this order instead of aia_i.

    More formally, let a1,a2,…,ai,…,aka_1, a_2, \ldots, a_i, \ldots, a_k be an array aa before the operation. After the operation, it becomes equal to a1,a2,…,ai−1,x,ai−x,ai+1,…,aka_1, a_2, \ldots, a_{i-1}, x, a_i - x, a_{i+1}, \ldots, a_k. Note that the length of aa increases by 11 on each operation.

Milena can perform this operation multiple times (possibly zero). She wants you to determine the minimum number of times she should perform this operation to make array aa non-decreasing.

An array x1,x2,…,xkx_1, x_2, \ldots, x_k of length kk is called non-decreasing if xi≤xi+1x_i \le x_{i+1} for all 1≤i<k1 \le i \lt k.

米莱娜从一位神秘爱慕者那里收到了一个长度为 nn 的整数数组 a1,a2,…,ana_1, a_2, \ldots, a_n。她认为,将该数组变为非递减序列或许有助于她识别这位神秘爱慕者。

她可以使用以下操作使该数组变为非递减序列:

  • 选择数组 aa 中的一个元素 aia_i 和一个整数 xx,满足 1≤x<ai1 \le x \lt a_i;然后将 aia_i 替换为两个元素 xx 和 ai−xa_i - x(按此顺序)插入到数组 aa 中。

    更准确地说,设操作前的数组 aa 为 a1,a2,…,ai,…,aka_1, a_2, \ldots, a_i, \ldots, a_k,则操作后数组变为 a1,a2,…,ai−1,x,ai−x,ai+1,…,aka_1, a_2, \ldots, a_{i-1}, x, a_i - x, a_{i+1}, \ldots, a_k。注意:每次操作会使数组 aa 的长度增加 11。

米莱娜可以执行该操作多次(也可以不执行)。请你帮她确定:使数组 aa 变为非递减序列所需的最少操作次数。

长度为 kk 的数组 x1,x2,…,xkx_1, x_2, \ldots, x_k 称为非递减的,当且仅当对所有 1≤i<k1 \le i \lt k 均满足 xi≤xi+1x_i \le x_{i+1}。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10 0001 \leq t \leq 10\,000). The description of test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051\leq n\leq 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\leq a_i\leq 10^9) – the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10 0001 \leq t \leq 10\,000)。随后是各测试用例的描述。

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

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

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

输出格式

For each test case, output one integer — the minimum number of operations required to make the array non-decreasing.

It can be shown that it is always possible to make the array aa non-decreasing in the finite number of operations.

对于每个测试用例,输出一个整数——使数组非递减所需的最少操作次数。

可以证明:总能在有限次操作内使数组 aa 变为非递减。

输入输出样例

  • 输入#1

    4
    3
    1 3 2
    4
    1 2 3 4
    3
    3 2 1
    7
    1 4 4 3 5 7 6

    输出#1

    1
    0
    3
    9

说明/提示

In the first test case, Milena can replace the second element of array aa by integers 11 and 22, so the array would become [ 1, 1‾, 2‾, 2 ][\, 1, \, \underline{1}, \, \underline{2}, \, 2 \,]. Only 11 operation is required.

In the second test case, the array aa is already non-decreasing, so the answer is 00.

In the third test case, Milena can make array aa non-decreasing in 33 operations as follows.

  • Select i=1i=1 and x=2x=2 and replace a1a_1 by 22 and 11. The array aa becomes equal to [ 2‾, 1‾, 2, 1 ][\, \underline{2}, \, \underline{1}, \, 2, \, 1 \, ].
  • Select i=3i=3 and x=1x=1 and replace a3a_3 by 11 and 11. The array aa becomes equal to [ 2, 1, 1‾, 1‾, 1 ][\, 2, \, 1, \, \underline{1}, \, \underline{1}, \, 1 \,].
  • Select i=1i=1 and x=1x=1 and replace a1a_1 by 22 and 11. The array aa becomes equal to [ 1‾, 1‾, 1, 1, 1, 1 ][\, \underline{1}, \, \underline{1}, \, 1, \, 1, \, 1, \, 1 \,].

It can be shown that it is impossible to make it non-decreasing in 22 or less operations, so the answer is 33.

在第一个测试用例中,米莲娜可以将数组 aa 的第二个元素替换为整数 11 和 22,从而使数组变为 [ 1, 1‾, 2‾, 2 ][\, 1, \, \underline{1}, \, \underline{2}, \, 2 \,]。仅需 11 次操作。

在第二个测试用例中,数组 aa 已经是非递减的,因此答案为 00。

在第三个测试用例中,米莲娜可以通过 33 次操作使数组 aa 变为非递减数组,具体如下:

  • 选择 i=1i=1 和 x=2x=2,将 a1a_1 替换为 22 和 11。数组 aa 变为 [ 2‾, 1‾, 2, 1 ][\, \underline{2}, \, \underline{1}, \, 2, \, 1 \, ]。
  • 选择 i=3i=3 和 x=1x=1,将 a3a_3 替换为 11 和 11。数组 aa 变为 [ 2, 1, 1‾, 1‾, 1 ][\, 2, \, 1, \, \underline{1}, \, \underline{1}, \, 1 \,]。
  • 选择 i=1i=1 和 x=1x=1,将 a1a_1 替换为 22 和 11。数组 aa 变为 [ 1‾, 1‾, 1, 1, 1, 1 ][\, \underline{1}, \, \underline{1}, \, 1, \, 1, \, 1, \, 1 \,]。

可以证明,无法在 22 次或更少的操作内使其变为非递减数组,因此答案为 33。

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

首页