CF1649A.Game

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are playing a very popular computer game. The next level consists of nn consecutive locations, numbered from 11 to nn, 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 ii to any location with land i+xi + x, spending xx coins (x≥0x \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.

你正在玩一款非常流行的电脑游戏。下一关由 nn 个连续的位置组成,编号从 11 到 nn,每个位置要么是陆地,要么是水。已知第一个和最后一个位置均为陆地;要通关,你必须从第一个位置移动到最后一个位置。此外,一旦你进入一个水的位置,你就会死亡,因此你只能在陆地位置之间移动。

你可以免费在相邻位置之间跳跃;此外,你最多可以进行一次长跳:从任意一个陆地位置 ii 跳到另一个陆地位置 i+xi + x(其中 x≥0x \geq 0),花费 xx 枚金币。

你的任务是以最少的金币花费,从第一个位置移动到最后一个位置。

注意:这总是可行的,因为第一个和最后一个位置均为陆地。

输入格式

There are several test cases in the input data. The first line contains a single integer tt (1≤t≤1001 \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 nn (2≤n≤1002 \leq n \leq 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_n (0≤ai≤10 \leq a_i \leq 1), where ai=1a_i = 1 means that the ii-th location is the location with land, and ai=0a_i = 0 means that the ii-th location is the location with water. It is guaranteed that a1=1a_1 = 1 and an=1a_n = 1.

输入数据包含多个测试用例。第一行包含一个整数 tt(1≤t≤1001 \leq t \leq 100),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1002 \leq n \leq 100),表示位置的数量。

每个测试用例的第二行包含一个整数序列 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤10 \leq a_i \leq 1),其中 ai=1a_i = 1 表示第 ii 个位置为陆地,ai=0a_i = 0 表示第 ii 个位置为水域。保证 a1=1a_1 = 1 且 an=1a_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 00.

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 44 coins.

In the third test case, you can jump from the first location to the third for 22 coins, and then jump to the fourth location for free, so the answer is 22. It can be shown that this is the optimal way.

在第一个测试用例中,只需从第一个位置免费跳转到第二个位置(同时也是最后一个位置),因此答案为 00。

在第二个测试用例中,从第一个位置移动到最后一个位置的唯一方式是直接在这两个位置之间跳跃,花费 44 枚硬币。

在第三个测试用例中,你可以先从第一个位置跳到第三个位置,花费 22 枚硬币,再从第三个位置免费跳到第四个位置,因此答案为 22。可以证明这是最优方案。

输入解题思路,AI测评打分。不知道怎么写?

首页