欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1433B.Yet Another Bookshelf
Yet Another Bookshelf
There is a bookshelf which can fit books. The -th position of bookshelf is if there is a book on this position and otherwise. It is guaranteed that there is at least one book on the bookshelf.
In one move, you can choose some contiguous segment consisting of books (i.e. for each from to the condition holds) and:
- Shift it to the right by : move the book at index to for all . This move can be done only if and there is no book at the position .
- Shift it to the left by : move the book at index to for all . This move can be done only if and there is no book at the position .
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 there is a gap between books ( when and ), for there are no gaps between books and for there are also no gaps between books.
You have to answer independent test cases.
Input
The first line of the input contains one integer () — the number of test cases. Then test cases follow.
The first line of the test case contains one integer () — the number of places on a bookshelf. The second line of the test case contains integers (), where is if there is a book at this position and 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).
Note
In the first test case of the example, you can shift the segment to the right and the segment to the right. After all moves, the books form the contiguous segment . So the answer is .
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 to the left and then the segment to the left again. After all moves, the books form the contiguous segment . So the answer is .
In the fourth test case of the example, you can shift the segment to the right, the segment to the right, the segment to the left and then the segment to the left. After all moves, the books form the contiguous segment . So the answer is .
In the fifth test case of the example, you can shift the segment to the right. After all moves, the books form the contiguous segment . So the answer is .
Samples
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
2
0
2
4
1
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |