CF1705B.Mark the Dust Sweeper
入门
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Mark is cleaning a row of n rooms. The i-th room has a nonnegative dust level ai. He has a magical cleaning machine that can do the following three-step operation.
- Select two indices i<j such that the dust levels ai, ai+1, …, aj−1 are all strictly greater than 0.
- Set ai to ai−1.
- Set aj to aj+1.
Mark's goal is to make a1=a2=…=an−1=0 so that he can nicely sweep the n-th room. Determine the minimum number of operations needed to reach his goal.
马克正在清理一排 n 个房间。第 i 个房间的灰尘量为非负整数 ai。他拥有一台神奇的清洁机器,可以执行如下三步操作:
- 选择两个下标 i<j,使得灰尘量 ai, ai+1, …, aj−1 全部严格大于 0;
- 将 ai 减 1(即设为 ai−1);
- 将 aj 加 1(即设为 aj+1)。
马克的目标是使 a1=a2=…=an−1=0,从而能干净利落地清扫第 n 个房间。请确定达成该目标所需的最少操作次数。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the number of rooms.
The second line of each test case contains n integers a1, a2, ..., an (0≤ai≤109) — the dust level of each room.
It is guaranteed that the sum of n across all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)——测试用例的数量。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)——房间的数量。
每个测试用例的第二行包含 n 个整数 a1, a2, ..., an(0≤ai≤109)——每个房间的灰尘量。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, print a line containing a single integer — the minimum number of operations. It can be proven that there is a sequence of operations that meets the goal.
对于每个测试用例,输出一行,包含一个整数——即最少的操作次数。可以证明,存在一个操作序列能够达成目标。
输入输出样例
输入#1
4 3 2 0 0 5 0 2 0 2 0 6 2 0 3 0 4 6 4 0 0 0 10
输出#1
3 5 11 0
说明/提示
In the first case, one possible sequence of operations is as follows.
- Choose i=1 and j=2, yielding the array [1,1,0].
- Choose i=1 and j=3, yielding the array [0,1,1].
- Choose i=2 and j=3, yielding the array [0,0,2].
At this point, a1=a2=0, completing the process.
In the second case, one possible sequence of operations is as follows.
- Choose i=4 and j=5, yielding the array [0,2,0,1,1].
- Choose i=2 and j=3, yielding the array [0,1,1,1,1].
- Choose i=2 and j=5, yielding the array [0,0,1,1,2].
- Choose i=3 and j=5, yielding the array [0,0,0,1,3].
- Choose i=4 and j=5, yielding the array [0,0,0,0,4].
In the last case, the array already satisfies the condition.
在第一种情况下,一种可能的操作序列如下:
- 选择 i=1 和 j=2,得到数组 [1,1,0]。
- 选择 i=1 和 j=3,得到数组 [0,1,1]。
- 选择 i=2 和 j=3,得到数组 [0,0,2]。
此时,a1=a2=0,过程完成。
在第二种情况下,一种可能的操作序列如下:
- 选择 i=4 和 j=5,得到数组 [0,2,0,1,1]。
- 选择 i=2 和 j=3,得到数组 [0,1,1,1,1]。
- 选择 i=2 和 j=5,得到数组 [0,0,1,1,2]。
- 选择 i=3 和 j=5,得到数组 [0,0,0,1,3]。
- 选择 i=4 和 j=5,得到数组 [0,0,0,0,4]。
在最后一种情况下,数组已满足条件。
输入解题思路,AI测评打分。不知道怎么写?