CF1705B.Mark the Dust Sweeper

普及/提高-

通过率:0%

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<j such that the dust levels aia_i , ai+1a_{i+1} , \dots , aj1a_{j-1} are all strictly greater than 00 .
  • Set aia_i to ai1a_i-1 .
  • Set aja_j to aj+1a_j+1 .

Mark's goal is to make a1=a2==an1=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.

输入格式

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

The first line of each test case contains a single integer nn ( 2n21052\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 ( 0ai1090\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 21052\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.

首页