CF1433B.Yet Another Bookshelf

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There is a bookshelf which can fit nn books. The ii-th position of bookshelf is ai=1a_i = 1 if there is a book on this position and ai=0a_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][l; r] consisting of books (i.e. for each ii from ll to rr the condition ai=1a_i = 1 holds) and:

  • Shift it to the right by 11: move the book at index ii to i+1i + 1 for all l≤i≤rl \le i \le r. This move can be done only if r+1≤nr+1 \le n and there is no book at the position r+1r+1.
  • Shift it to the left by 11: move the book at index ii to i−1i-1 for all l≤i≤rl \le i \le r. This move can be done only if l−1≥1l-1 \ge 1 and there is no book at the position l−1l-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]a = [0, 0, 1, 0, 1] there is a gap between books (a4=0a_4 = 0 when a3=1a_3 = 1 and a5=1a_5 = 1), for a=[1,1,0]a = [1, 1, 0] there are no gaps between books and for a=[0,0,0]a = [0, 0,0] there are also no gaps between books.

You have to answer tt independent test cases.

书架上可以放置 nn 本书。若第 ii 个位置上有书,则 ai=1a_i = 1;否则 ai=0a_i = 0。保证书架上至少有一本书。

在一次操作中,你可以选择一段连续的、全部由书构成的区间 [l;r][l; r](即对所有 l≤i≤rl \le i \le r,均有 ai=1a_i = 1),并执行以下两种操作之一:

  • 向右平移 1 位:将位置 ii 上的书移动到位置 i+1i + 1(对所有 l≤i≤rl \le i \le r)。该操作仅当 r+1≤nr+1 \le n 且位置 r+1r+1 上没有书(即 ar+1=0a_{r+1} = 0)时才可进行。
  • 向左平移 1 位:将位置 ii 上的书移动到位置 i−1i-1(对所有 l≤i≤rl \le i \le r)。该操作仅当 l−1≥1l-1 \ge 1 且位置 l−1l-1 上没有书(即 al−1=0a_{l-1} = 0)时才可进行。

你的任务是求出使书架上所有书变为一段连续(无空隙) 区间的最少操作次数。

例如,对于 a=[0,0,1,0,1]a = [0, 0, 1, 0, 1],两本书之间存在空隙(a3=1a_3 = 1,a4=0a_4 = 0,a5=1a_5 = 1);对于 a=[1,1,0]a = [1, 1, 0],书之间没有空隙;对于 a=[0,0,0]a = [0, 0, 0],同样没有空隙(因为其中没有书,但题目已保证至少有一本书,故此例仅作概念说明)。

你需要回答 tt 个相互独立的测试用例。

输入格式

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

The first line of the test case contains one integer nn (1≤n≤501 \le n \le 50) — the number of places on a bookshelf. The second line of the test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤10 \le a_i \le 1), where aia_i is 11 if there is a book at this position and 00 otherwise. It is guaranteed that there is at least one book on the bookshelf.

输入的第一行包含一个整数 tt(1≤t≤2001 \le t \le 200),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤501 \le n \le 50),表示书架上的位置数量。每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤10 \le a_i \le 1),其中 ai=1a_i = 1 表示该位置有一本书,ai=0a_i = 0 表示该位置没有书。保证书架上至少有一本书。

输出格式

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][3; 3] to the right and the segment [4;5][4; 5] to the right. After all moves, the books form the contiguous segment [5;7][5; 7]. So the answer is 22.

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][5; 5] to the left and then the segment [4;4][4; 4] to the left again. After all moves, the books form the contiguous segment [1;3][1; 3]. So the answer is 22.

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

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

在示例的第一个测试用例中,你可以将线段 [3;3][3; 3] 向右移动,同时将线段 [4;5][4; 5] 向右移动。所有移动完成后,书籍构成连续的线段 [5;7][5; 7]。因此答案为 22。

在示例的第二个测试用例中,你无需进行任何操作,书架上的所有书籍已经构成了一个连续的线段。

在示例的第三个测试用例中,你可以先将线段 [5;5][5; 5] 向左移动,再将线段 [4;4][4; 4] 向左移动。所有移动完成后,书籍构成连续的线段 [1;3][1; 3]。因此答案为 22。

在示例的第四个测试用例中,你可以将线段 [1;1][1; 1] 向右移动、线段 [2;2][2; 2] 向右移动、线段 [6;6][6; 6] 向左移动,然后将线段 [5;5][5; 5] 向左移动。所有移动完成后,书籍构成连续的线段 [3;4][3; 4]。因此答案为 44。

在示例的第五个测试用例中,你可以将线段 [1;2][1; 2] 向右移动。所有移动完成后,书籍构成连续的线段 [2;5][2; 5]。因此答案为 11。

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

首页