CF2260A.Monocarp's Contest

入门

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Monocarp is preparing a team programming contest. The contest has nn problems, each of which is either easy or hard. The problems are numbered from 11 to nn.

Monocarp wants the first and the last problems of the contest to be easy. In one operation, he can choose any two problems and swap them.

Determine the minimum number of operations required to make the first and the last problems easy, or report that it is impossible.

Monocarp 正在筹备一场团队编程竞赛。该竞赛共有 nn 道题目,每道题要么是简单题,要么是难题。题目编号从 11 到 nn。

Monocarp 希望竞赛的第一题和最后一题均为简单题。在一次操作中,他可以任选两道题并交换它们的位置。

请确定使第一题和最后一题均为简单题所需的最少操作次数;若不可能实现,请报告这一点。

输入格式

The first line contains an integer tt (1≤t≤1031 \le t \le 10^3) — the number of test cases.

Each test case consists of two lines

  • the first line contains one integer nn (2≤n≤502 \le n \le 50) — the number of problems in the contest;
  • the second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤10 \le a_i \le 1). If ai=0a_i=0, then the problem with number ii is easy; if ai=1a_i=1, then it is hard.

第一行包含一个整数 tt(1≤t≤1031 \le t \le 10^3)—— 测试用例的数量。

每个测试用例由两行组成:

  • 第一行包含一个整数 nn(2≤n≤502 \le n \le 50)—— 比赛中问题的数量;
  • 第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤10 \le a_i \le 1)。若 ai=0a_i=0,则编号为 ii 的问题为简单题;若 ai=1a_i=1,则该题为难题。

输出格式

For each test case, print the minimum number of operations required to make the first and the last problems easy. If it is impossible to satisfy the requirement, print −1-1.

对于每个测试用例,输出使第一个和最后一个题目变为简单题所需的最少操作次数。如果无法满足该要求,则输出 −1-1。

输入输出样例

  • 输入#1

    4
    2
    0 0
    2
    0 1
    6
    1 0 0 1 0 0
    5
    1 0 0 1 1

    输出#1

    0
    -1
    1
    2

说明/提示

In the first test case, the first and the last problems are already easy, so no operations are needed.

In the second test case, there is only one easy problem, so it is impossible to make both the first and the last problems easy.

In the third test case, one can swap the first and the second problems.

In the fourth test case, one can first swap the first and the second problems, and then — the third and the fifth problems.

在第一个测试用例中,第一题和最后一题已经是简单题,因此无需任何操作。

在第二个测试用例中,仅有一道简单题,因此无法使第一题和最后一题同时变为简单题。

在第三个测试用例中,可以交换第一题和第二题。

在第四个测试用例中,可以先交换第一题和第二题,再交换第三题和第五题。

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

首页