codeforces-1433B

Yet Another Bookshelf

There is a bookshelf which can fit n books. The i-th position of bookshelf is ai=1 if there is a book on this position and ai=0 otherwise. It is guaranteed that there is at least one book on the bookshelf.

In one move, you can choose some contiguous segment [l;r] consisting of books (i.e. for each i from l to r the condition ai=1 holds) and:

Shift it to the right by 1: move the book at index i to i+1 for all l≤i≤r. This move can be done only if r+1≤n and there is no book at the position r+1.
Shift it to the left by 1: move the book at index i to i−1 for all l≤i≤r. This move can be done only if l−1≥1 and there is no book at the position l−1.
Your task is to find the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without any gaps).

For example, for a=[0,0,1,0,1] there is a gap between books (a4=0 when a3=1 and a5=1), for a=[1,1,0] there are no gaps between books and for a=[0,0,0] there are also no gaps between books.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤200) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤50) — the number of places on a bookshelf. The second line of the test case contains n integers a1,a2,…,an (0≤ai≤1), where ai is 1 if there is a book at this position and 0 otherwise. It is guaranteed that there is at least one book on the bookshelf.

Output
For each test case, print one integer: the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without gaps).

Example
Input
5
7
0 0 1 0 1 0 1
3
1 0 0
5
1 1 0 0 1
6
1 0 0 0 0 1
5
1 1 0 1 1
Output
2
0
2
4
1
Note
In the first test case of the example, you can shift the segment [3;3] to the right and the segment [4;5] to the right. After all moves, the books form the contiguous segment [5;7]. So the answer is 2.

In the second test case of the example, you have nothing to do, all the books on the bookshelf form the contiguous segment already.

In the third test case of the example, you can shift the segment [5;5] to the left and then the segment [4;4] to the left again. After all moves, the books form the contiguous segment [1;3]. So the answer is 2.

In the fourth test case of the example, you can shift the segment [1;1] to the right, the segment [2;2] to the right, the segment [6;6] to the left and then the segment [5;5] to the left. After all moves, the books form the contiguous segment [3;4]. So the answer is 4.

In the fifth test case of the example, you can shift the segment [1;2] to the right. After all moves, the books form the contiguous segment [2;5]. So the answer is 1.

#include"iostream"
#include"stack"
using namespace std;
int main()
{
	int t,n;
	cin>>t;
	int f;
	int sum,num;
	stack<int>mystack;
	while(t--)
	{
		cin>>n;
		sum = 0;
		num = 0;
		for(int i = 0;i<n;i++)
		{
			cin>>f;
			if(f==1)
			{
				if(mystack.empty())
				{
					mystack.push(f);
				}			
				else
				{
					num+=sum;
					sum = 0;
				}
			}
			else if(f == 0&&!mystack.empty())
				sum++;
		}
		cout<<num<<endl;
		while(!mystack.empty())
			mystack.pop();
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值