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, 𝑎𝑖+1ai+1, ……, 𝑎𝑗−1aj−1 are all strictly greater than 00.
- Set 𝑎𝑖ai to 𝑎𝑖−1ai−1.
- Set 𝑎𝑗aj to 𝑎𝑗+1aj+1.
Mark's goal is to make 𝑎1=𝑎2=…=𝑎𝑛−1=0a1=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.
Input
The first line contains a single integer 𝑡t (1≤𝑡≤1041≤t≤104) — the number of test cases.
The first line of each test case contains a single integer 𝑛n (2≤𝑛≤2⋅1052≤n≤2⋅105) — the number of rooms.
The second line of each test case contains 𝑛n integers 𝑎1a1, 𝑎2a2, ..., 𝑎𝑛an (0≤𝑎𝑖≤1090≤ai≤109) — the dust level of each room.
It is guaranteed that the sum of 𝑛n across all test cases does not exceed 2⋅1052⋅105.
Output
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.
Example
input
Copy
4
3
2 0 0
5
0 2 0 2 0
6
2 0 3 0 4 6
4
0 0 0 10
output
Copy
3 5 11 0
Note
In the first case, one possible sequence of operations is as follows.
- Choose 𝑖=1i=1 and 𝑗=2j=2, yielding the array [1,1,0][1,1,0].
- Choose 𝑖=1i=1 and 𝑗=3j=3, yielding the array [0,1,1][0,1,1].
- Choose 𝑖=2i=2 and 𝑗=3j=3, yielding the array [0,0,2][0,0,2].
At this point, 𝑎1=𝑎2=0a1=a2=0, completing the process.
In the second case, one possible sequence of operations is as follows.
- Choose 𝑖=4i=4 and 𝑗=5j=5, yielding the array [0,2,0,1,1][0,2,0,1,1].
- Choose 𝑖=2i=2 and 𝑗=3j=3, yielding the array [0,1,1,1,1][0,1,1,1,1].
- Choose 𝑖=2i=2 and 𝑗=5j=5, yielding the array [0,0,1,1,2][0,0,1,1,2].
- Choose 𝑖=3i=3 and 𝑗=5j=5, yielding the array [0,0,0,1,3][0,0,0,1,3].
- Choose 𝑖=4i=4 and 𝑗=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和j使得a【i】-1,a【j】+1,但是,在移动过程中要保证,a【j】>0;
那么长的题目就这么点信息
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<map>
#include<cstring>
#include<queue>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 2e5 + 5, INF = 0x3f3f3f3f;
ll gcdd(ll a, ll b)
{
if (b) while ((a %= b) && (b %= a));
return a + b;
}
ll t,n,m,a,b,cnt,ans;
ll arr[N];
int main()
{
cin>>t;
while(t--)
{
cin>>n;
ans=0;
ll flag=0;
cnt=0;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
if(arr[i]!=0)
{
cnt=1;
}
if(cnt==1 && i<n )
{
if(arr[i]==0)
{
ans++;
}else{
ans+=arr[i];
}
}
}
cout<<ans<<endl;
}
}