A14920 | Game
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are playing a very popular computer game. The next level consists of $n$ consecutive locations, numbered from $1$ to $n$ , 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 $i$ to any location with land $i + x$ , spending $x$ coins ( $x \geq 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.
You can jump between adjacent locations for free, as well as no more than once jump from any location with land $i$ to any location with land $i + x$ , spending $x$ coins ( $x \geq 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.
输入格式
There are several test cases in the input data. The first line contains a single integer $t$ ( $1 \leq t \leq 100$ ) — the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer $n$ ( $2 \leq n \leq 100$ ) — the number of locations.
The second line of the test case contains a sequence of integers $a_1, a_2, \ldots, a_n$ ( $0 \leq a_i \leq 1$ ), where $a_i = 1$ means that the $i$ -th location is the location with land, and $a_i = 0$ means that the $i$ -th location is the location with water. It is guaranteed that $a_1 = 1$ and $a_n = 1$ .
The first line of each test case contains one integer $n$ ( $2 \leq n \leq 100$ ) — the number of locations.
The second line of the test case contains a sequence of integers $a_1, a_2, \ldots, a_n$ ( $0 \leq a_i \leq 1$ ), where $a_i = 1$ means that the $i$ -th location is the location with land, and $a_i = 0$ means that the $i$ -th location is the location with water. It is guaranteed that $a_1 = 1$ and $a_n = 1$ .
输出格式
For each test case print a single integer — the answer to the problem.
输入输出样例
输入 #1
3 2 1 1 5 1 0 1 0 1 4 1 0 1 1
输出 #1
0 4 2
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 $0$ .
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 $4$ coins.
In the third test case, you can jump from the first location to the third for $2$ coins, and then jump to the fourth location for free, so the answer is $2$ . It can be shown that this is the optimal way.
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 $4$ coins.
In the third test case, you can jump from the first location to the third for $2$ coins, and then jump to the fourth location for free, so the answer is $2$ . It can be shown that this is the optimal way.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted