CF1923A.Moving Chips
入门
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a ribbon divided into n cells, numbered from 1 to n from left to right. Each cell either contains a chip or is free.
You can perform the following operation any number of times (possibly zero): choose a chip and move it to the closest free cell to the left. You can choose any chip that you want, provided that there is at least one free cell to the left of it. When you move the chip, the cell where it was before the operation becomes free.
Your goal is to move the chips in such a way that they form a single block, without any free cells between them. What is the minimum number of operations you have to perform?
有一条被划分为 n 个格子的丝带,格子从左到右编号为 1 到 n。每个格子中要么放置了一个棋子,要么为空。
你可以执行以下操作任意多次(也可以不执行):选择一个棋子,并将其向左移动到其左侧最近的空格子中。你可以任选一个棋子进行该操作,前提是该棋子左侧至少存在一个空格子。当棋子被移动后,它原先所在的格子变为空格子。
你的目标是通过移动棋子,使得所有棋子连成一个连续的块(即棋子占据的格子之间没有空格子)。问:最少需要执行多少次操作?
输入格式
The first line contains one integer t (1≤t≤1000) — the number of test cases.
Each test case consists of two lines:
- the first line contains one integer n (2≤n≤50) — the number of cells;
- the second line contains n integers a1,a2,…,an (0≤ai≤1); ai=0 means that the i-th cell is free; ai=1 means that the i-th cell contains a chip.
Additional constraint on the input: in each test case, at least one cell contains a chip.
第一行包含一个整数 t(1≤t≤1000)—— 表示测试用例的数量。
每个测试用例由两行组成:
- 第一行包含一个整数 n(2≤n≤50)—— 表示单元格的数量;
- 第二行包含 n 个整数 a1,a2,…,an(0≤ai≤1);其中 ai=0 表示第 i 个单元格为空;ai=1 表示第 i 个单元格中有一个棋子。
输入的额外约束:在每个测试用例中,至少有一个单元格中包含棋子。
输出格式
For each test case, print one integer — the minimum number of operations you have to perform so that all chips form a single block without any free cells between them.
对于每个测试用例,输出一个整数——使得所有筹码形成一个连续的块(块内无空单元格)所需的最少操作次数。
输入输出样例
输入#1
5 8 0 1 1 1 0 1 1 0 6 0 1 0 0 0 0 6 1 1 1 1 1 1 5 1 0 1 0 1 9 0 1 1 0 0 0 1 1 0
输出#1
1 0 0 2 3
说明/提示
In the first example, you can perform the operation on the chip in the 7-th cell. The closest free cell to the left is the 5-th cell, so it moves there. After that, all chips form a single block.
In the second example, all chips are already in a single block. Same for the third example.
在第一个例子中,你可以对第 7 个格子中的棋子执行操作。其左侧最近的空格子是第 5 个格子,因此它移动到该位置。此后,所有棋子形成一个连续的块。
在第二个例子中,所有棋子已经构成一个连续的块。第三个例子同理。
输入解题思路,AI测评打分。不知道怎么写?