CF1705B.Mark the Dust Sweeper

入门

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

Mark is cleaning a row of nn rooms. The ii-th room has a nonnegative dust level aia_i. He has a magical cleaning machine that can do the following three-step operation.

  • Select two indices i<ji \lt j such that the dust levels aia_i, ai+1a_{i+1}, …\dots, aj−1a_{j-1} are all strictly greater than 00.
  • Set aia_i to ai−1a_i-1.
  • Set aja_j to aj+1a_j+1.

Mark's goal is to make a1=a2=…=an−1=0a_1 = a_2 = \ldots = a_{n-1} = 0 so that he can nicely sweep the nn-th room. Determine the minimum number of operations needed to reach his goal.

马克正在清理一排 nn 个房间。第 ii 个房间的灰尘量为非负整数 aia_i。他拥有一台神奇的清洁机器,可以执行如下三步操作:

  • 选择两个下标 i<ji \lt j,使得灰尘量 aia_i, ai+1a_{i+1}, …\dots, aj−1a_{j-1} 全部严格大于 00;
  • 将 aia_i 减 11(即设为 ai−1a_i-1);
  • 将 aja_j 加 11(即设为 aj+1a_j+1)。

马克的目标是使 a1=a2=…=an−1=0a_1 = a_2 = \ldots = a_{n-1} = 0,从而能干净利落地清扫第 nn 个房间。请确定达成该目标所需的最少操作次数。

输入格式

The first line contains a single integer tt (1≤t≤1041\leq t\leq 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052\leq n\leq 2\cdot 10^5) — the number of rooms.

The second line of each test case contains nn integers a1a_1, a2a_2, ..., ana_n (0≤ai≤1090\leq a_i\leq 10^9) — the dust level of each room.

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

第一行包含一个整数 tt(1≤t≤1041\leq t\leq 10^4)——测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052\leq n\leq 2\cdot 10^5)——房间的数量。

每个测试用例的第二行包含 nn 个整数 a1a_1, a2a_2, ..., ana_n(0≤ai≤1090\leq a_i\leq 10^9)——每个房间的灰尘量。

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

输出格式

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=1i=1 and j=2j=2, yielding the array [1,1,0][1,1,0].
  • Choose i=1i=1 and j=3j=3, yielding the array [0,1,1][0,1,1].
  • Choose i=2i=2 and j=3j=3, yielding the array [0,0,2][0,0,2].

At this point, a1=a2=0a_1=a_2=0, completing the process.

In the second case, one possible sequence of operations is as follows.

  • Choose i=4i=4 and j=5j=5, yielding the array [0,2,0,1,1][0,2,0,1,1].
  • Choose i=2i=2 and j=3j=3, yielding the array [0,1,1,1,1][0,1,1,1,1].
  • Choose i=2i=2 and j=5j=5, yielding the array [0,0,1,1,2][0,0,1,1,2].
  • Choose i=3i=3 and j=5j=5, yielding the array [0,0,0,1,3][0,0,0,1,3].
  • Choose i=4i=4 and j=5j=5, yielding the array [0,0,0,0,4][0,0,0,0,4].

In the last case, the array already satisfies the condition.

在第一种情况下,一种可能的操作序列如下:

  • 选择 i=1i=1 和 j=2j=2,得到数组 [1,1,0][1,1,0]。
  • 选择 i=1i=1 和 j=3j=3,得到数组 [0,1,1][0,1,1]。
  • 选择 i=2i=2 和 j=3j=3,得到数组 [0,0,2][0,0,2]。

此时,a1=a2=0a_1=a_2=0,过程完成。

在第二种情况下,一种可能的操作序列如下:

  • 选择 i=4i=4 和 j=5j=5,得到数组 [0,2,0,1,1][0,2,0,1,1]。
  • 选择 i=2i=2 和 j=3j=3,得到数组 [0,1,1,1,1][0,1,1,1,1]。
  • 选择 i=2i=2 和 j=5j=5,得到数组 [0,0,1,1,2][0,0,1,1,2]。
  • 选择 i=3i=3 和 j=5j=5,得到数组 [0,0,0,1,3][0,0,0,1,3]。
  • 选择 i=4i=4 和 j=5j=5,得到数组 [0,0,0,0,4][0,0,0,0,4]。

在最后一种情况下,数组已满足条件。

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

首页