CF2227E.It All Went Sideways

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Yousef has nn columns of cubes standing side by side. The ii-th column contains aia_i identical unit cubes stacked vertically. Initially gravity pulls cubes downwards, so every column ii contains exactly aia_i cubes at heights 1,2,…,ai1, 2, \dots, a_i.

Suddenly, gravity shifts to the right. Each cube slides horizontally as far to the right as possible. A cube cannot pass through or overlap other cubes, and it must remain at its original height. The final configuration is uniquely determined by the initial heights.

Before and after applying rightward gravity.

Before the gravity shift, Yousef may perform at most one operation: choose an index ii and decrease aia_i by 11 (i.e. remove one cube from that column). He may also choose to do nothing.

A cube is said to move if its column index after the gravity shift is different from its original column index.

Find the maximum possible number of cubes that will move after the gravity shift, assuming Yousef applies the single decrease optimally (or chooses not to apply it).

优素福有 nn 列立方体并排竖立。第 ii 列包含 aia_i 个相同的单位立方体,垂直堆叠。初始时重力方向向下,因此每列 ii 中恰好有 aia_i 个立方体,分别位于高度 1,2,…,ai1, 2, \dots, a_i 处。

突然,重力方向变为向右。每个立方体尽可能向右水平滑动。立方体不能穿过或重叠其他立方体,且必须保持其原始高度。最终构型由初始高度唯一确定。

施加向右重力前后的状态。

在重力方向改变前,优素福最多可执行一次操作:选择一个下标 ii,将 aia_i 减少 11(即从此列移除一个立方体);他也可以选择不执行任何操作。

若一个立方体在重力方向改变后的列编号与其原始列编号不同,则称该立方体发生了移动。

假设优素福以最优方式(或选择不执行)执行至多一次减一操作,求重力方向改变后可能发生的最大移动立方体数量。

输入格式

The first line contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n) — the elements of the array.

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

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

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

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \le a_i \le n)—— 数组的元素。

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

输出格式

For each test case, output a single integer — the maximum possible number of cubes that will move after the gravity shift, assuming Yousef applies the single decrease optimally (or chooses not to apply it).

对于每个测试用例,输出一个整数——在优异地执行一次减小操作(或选择不执行该操作)的前提下,重力方向改变后可能移动的立方体的最大数量。

输入输出样例

  • 输入#1

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

    输出#1

    8
    12
    0
    10
    18

说明/提示

In the first test case, it is optimal to perform the operation on index 55, making the array a=[1,2,3,2,0]a = [1, 2, 3, 2, 0]. After the gravity shift, every remaining cube will move. The answer is 1+2+3+2=81+2+3+2 = 8. It can be proven that a greater answer does not exist.

In the second test case, we can perform the operation on index 66, making the array a=[5,4,1,1,1,0,3]a = [5, 4, 1, 1, 1, 0, 3]. After the gravity shift, 5+4+1+1+1=125+4+1+1+1 = 12 cubes will move. Note that the cubes at index 77 do not move, since they are already at the rightmost end of the array.

In the third test case, there is no way to make any cube move.

在第一个测试用例中,最优操作是将操作应用于下标 55,使得数组变为 a=[1,2,3,2,0]a = [1, 2, 3, 2, 0]。重力下落之后,所有剩余的方块都将下落。答案为 1+2+3+2=81+2+3+2 = 8。可以证明不存在更大的答案。

在第二个测试用例中,我们可以将操作应用于下标 66,使得数组变为 a=[5,4,1,1,1,0,3]a = [5, 4, 1, 1, 1, 0, 3]。重力下落之后,将有 5+4+1+1+1=125+4+1+1+1 = 12 个方块下落。注意,下标 77 处的方块不会下落,因为它们已位于数组最右端。

在第三个测试用例中,不存在使任何方块下落的方法。

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

首页