A13778 | Yet Another Bookshelf
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is a bookshelf which can fit $n$ books. The $i$ -th position of bookshelf is $a_i = 1$ if there is a book on this position and $a_i = 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 $a_i = 1$ holds) and:
- Shift it to the right by $1$ : move the book at index $i$ to $i + 1$ for all $l \le i \le r$ . This move can be done only if $r+1 \le 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 \le i \le r$ . This move can be done only if $l-1 \ge 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 ( $a_4 = 0$ when $a_3 = 1$ and $a_5 = 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.
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 $a_i = 1$ holds) and:
- Shift it to the right by $1$ : move the book at index $i$ to $i + 1$ for all $l \le i \le r$ . This move can be done only if $r+1 \le 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 \le i \le r$ . This move can be done only if $l-1 \ge 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 ( $a_4 = 0$ when $a_3 = 1$ and $a_5 = 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.
输入格式
The first line of the input contains one integer $t$ ( $1 \le t \le 200$ ) — the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ( $1 \le n \le 50$ ) — the number of places on a bookshelf. The second line of the test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $0 \le a_i \le 1$ ), where $a_i$ 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.
The first line of the test case contains one integer $n$ ( $1 \le n \le 50$ ) — the number of places on a bookshelf. The second line of the test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $0 \le a_i \le 1$ ), where $a_i$ 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.
输出格式
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).
输入输出样例
输入 #1
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
输出 #1
2 0 2 4 1
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted