CF2227F.It Just Keeps Going Sideways
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Yousef has n columns of cubes standing side by side. The i-th column contains ai identical unit cubes stacked vertically. Initially gravity pulls cubes downwards, so every column i contains exactly ai cubes at heights 1,2,…,ai.
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 i and decrease ai by 1 (i.e. remove one cube from that column). He may also choose to do nothing.
A cube's movement distance is defined as ∣j−i∣, where i is its original column index and j is its column index after the gravity shift.
Find the maximum possible total movement distance (the sum of the movement distances of all remaining cubes) Yousef can achieve.
优素福有 n 列立方体并排竖立。第 i 列包含 ai 个相同的单位立方体,垂直堆叠。初始时重力方向向下,因此每列 i 恰好包含 ai 个立方体,分别位于高度 1,2,…,ai 处。
突然,重力方向变为向右。每个立方体尽可能向右水平滑动。立方体不能穿过或重叠其他立方体,且必须保持其原始高度。最终构型由初始高度唯一确定。
施加向右重力前后的状态。
在重力方向改变前,优素福最多可执行一次操作:选择一个下标 i,将 ai 减少 1(即从此列移除一个立方体);他也可以选择不进行任何操作。
一个立方体的移动距离定义为 ∣j−i∣,其中 i 是其原始所在列的下标,j 是重力改变后其所处列的下标。
求优素福所能达到的最大总移动距离(即所有剩余立方体的移动距离之和)。
输入格式
The first line contains an integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 数组的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n)—— 数组的元素。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output a single integer — the maximum possible total movement distance (the sum of the movement distances of all remaining cubes) Yousef can achieve.
对于每个测试用例,输出一个整数——Yousef 能够达到的最大总移动距离(即所有剩余立方体的移动距离之和)。
输入输出样例
输入#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
9 37 0 17 29
说明/提示
In the first test case, the initial total movement distance is 5. If Yousef removes the cube at index 5, the array becomes [1,2,3,2,0]. Because the fifth column is now empty, all cubes from the first four columns are able to slide further to the right than they could originally. This results in a new total distance of 9.
In the third test case, the initial total movement distance is 0. Even if Yousef removes any cube, no remaining cube will be able to move. This results in a total distance of 0.
在第一个测试用例中,初始的总移动距离为 5。如果优素福移除索引为 5 处的方块,则数组变为 [1,2,3,2,0]。由于第五列现在为空,前四列中的所有方块均能比原先更向右滑动,从而得到新的总移动距离 9。
在第三个测试用例中,初始的总移动距离为 0。即使优素福移除任意一个方块,剩余的方块也无法再发生移动,因此总移动距离仍为 0。
输入解题思路,AI测评打分。不知道怎么写?