You are playing a very popular computer game. The next level consists of nnn consecutive locations, numbered from 111 to nnn, each of them containing either land or water. It is known that the first and last locations contain land, and for completing the level you have to move from the first location to the last. Also, if you become inside a location with water, you will die, so you can only move between locations with land.
You can jump between adjacent locations for free, as well as no more than once jump from any location with land iii to any location with land i+xi + xi+x, spending xxx coins (x≥0x \geq 0x≥0).
Your task is to spend the minimum possible number of coins to move from the first location to the last one.
Note that this is always possible since both the first and last locations are the land locations.
Input
There are several test cases in the input data. The first line contains a single integer ttt (1≤t≤1001 \leq t \leq 1001≤t≤100) — the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer nnn (2≤n≤1002 \leq n \leq 1002≤n≤100) — the number of locations.
The second line of the test case contains a sequence of integers a1,a2,…,ana_1, a_2, \ldots, a_na1,a2,…,an (0≤ai≤10 \leq a_i \leq 10≤ai≤1), where ai=1a_i = 1ai=1 means that the iii-th location is the location with land, and ai=0a_i = 0ai=0 means that the iii-th location is the location with water. It is guaranteed that a1=1a_1 = 1a1=1 and an=1a_n = 1an=1.
Output
For each test case print a single integer — the answer to the problem.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 2 1 1 5 1 0 1 0 1 4 1 0 1 1 | 0 4 2 |
Note
In the first test case, it is enough to make one free jump from the first location to the second one, which is also the last one, so the answer is 000.
In the second test case, the only way to move from the first location to the last one is to jump between them, which will cost 444 coins.
In the third test case, you can jump from the first location to the third for 222 coins, and then jump to the fourth location for free, so the answer is 222. It can be shown that this is the optimal way.
注意审题,题目中说的是一组数据中只能够跳一次!!!;
所以我们跳的时候要一次把所有水都跳过去,即从第一个0跳到最后一个0后面,才能够保证花费的钱是最少的。
#include<iostream>
#include<cstring>
using namespace std;
int a[110];
int main()
{
int t;cin>>t;
while(t--)
{
int n;cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
int l=-1,r=-1;
for(int i=1;i<=n;i++)
if(a[i]==0)
{
l=i-1;
break;
}
for(int j=n;j>=1;j--)
if(a[j]==0)
{
r=j+1;
break;
}
if(r==-1) cout<<"0"<<endl;
else cout<<r-l<<endl;
}
return 0;
}