CF1834A.Unit Array

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Given an array aa of length nn, which elements are equal to −1-1 and 11. Let's call the array aa good if the following conditions are held at the same time:

  • a1+a2+…+an≥0a_1 + a_2 + \ldots + a_n \ge 0;

  • a1⋅a2⋅…⋅an=1a_1 \cdot a_2 \cdot \ldots \cdot a_n = 1.

In one operation, you can select an arbitrary element of the array aia_i and change its value to the opposite. In other words, if ai=−1a_i = -1, you can assign the value to ai:=1a_i := 1, and if ai=1a_i = 1, then assign the value to ai:=−1a_i := -1.

Determine the minimum number of operations you need to perform to make the array aa good. It can be shown that this is always possible.

给定一个长度为 nn 的数组 aa,其元素均为 −1-1 或 11。若数组 aa 同时满足以下两个条件,则称其为“好”数组:

  • a1+a2+…+an≥0a_1 + a_2 + \ldots + a_n \ge 0;

  • a1⋅a2⋅…⋅an=1a_1 \cdot a_2 \cdot \ldots \cdot a_n = 1。

在一次操作中,你可以任选数组中的一个元素 aia_i,将其值变为相反数。即:若 ai=−1a_i = -1,则令 ai:=1a_i := 1;若 ai=1a_i = 1,则令 ai:=−1a_i := -1。

求使数组 aa 变为“好”数组所需的最少操作次数。可以证明,该目标总能达成。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤5001 \le t \le 500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1001 \le n \le 100) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (ai=±1a_i = \pm 1) — the elements of the array aa.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤5001 \le t \le 500),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \le n \le 100),表示数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(其中 ai=±1a_i = \pm 1),表示数组 aa 的元素。

输出格式

For each test case, output a single integer — the minimum number of operations that need to be done to make the aa array good.

对于每个测试用例,输出一个整数——使数组 aa 变为“好”的所需最少操作次数。

输入输出样例

  • 输入#1

    7
    4
    -1 -1 1 -1
    5
    -1 -1 -1 1 1
    4
    -1 1 -1 1
    3
    -1 -1 -1
    5
    1 1 1 1 1
    1
    -1
    2
    -1 -1

    输出#1

    1
    1
    0
    3
    0
    1
    2

说明/提示

In the first test case, we can assign the value a1:=1a_1 := 1. Then a1+a2+a3+a4=1+(−1)+1+(−1)=0≥0a_1 + a_2 + a_3 + a_4 = 1 + (-1) + 1 + (-1) = 0 \ge 0 and a1⋅a2⋅a3⋅a4=1⋅(−1)⋅1⋅(−1)=1a_1 \cdot a_2 \cdot a_3 \cdot a_4 = 1 \cdot (-1) \cdot 1 \cdot (-1) = 1. Thus, we performed 11 operation.

In the second test case, we can assign a1:=1a_1 := 1. Then a1+a2+a3+a4+a5=1+(−1)+(−1)+1+1=1≥0a_1 + a_2 + a_3 + a_4 + a_5 = 1 + (-1) + (-1) + 1 + 1 = 1 \ge 0 and a1⋅a2⋅a3⋅a4⋅a5=1⋅(−1)⋅(−1)⋅1⋅1=1a_1 \cdot a_2 \cdot a_3 \cdot a_4 \cdot a_5 = 1 \cdot (-1) \cdot (-1) \cdot 1 \cdot 1 = 1. Thus, we performed 11 operation.

In the third test case, a1+a2+a3+a4=(−1)+1+(−1)+1=0≥0a_1 + a_2 + a_3 + a_4 = (-1) + 1 + (-1) + 1 = 0 \ge 0 and a1⋅a2⋅a3⋅a4=(−1)⋅1⋅(−1)⋅1=1a_1 \cdot a_2 \cdot a_3 \cdot a_4 = (-1) \cdot 1 \cdot (-1) \cdot 1 = 1. Thus, all conditions are already satisfied and no operations are needed.

In the fourth test case, we can assign the values a1:=1,a2:=1,a3:=1a_1 := 1, a_2 := 1, a_3 := 1. Then a1+a2+a3=1+1+1=3≥0a_1 + a_2 + a_3 = 1 + 1 + 1 = 3 \ge 0 and a1⋅a2⋅a3=1⋅1⋅1=1a_1 \cdot a_2 \cdot a_3 = 1 \cdot 1 \cdot 1 = 1. Thus, we performed 33 operations.

在第一个测试用例中,我们可以将 a1:=1a_1 := 1。此时 a1+a2+a3+a4=1+(−1)+1+(−1)=0≥0a_1 + a_2 + a_3 + a_4 = 1 + (-1) + 1 + (-1) = 0 \ge 0,且 a1⋅a2⋅a3⋅a4=1⋅(−1)⋅1⋅(−1)=1a_1 \cdot a_2 \cdot a_3 \cdot a_4 = 1 \cdot (-1) \cdot 1 \cdot (-1) = 1。因此,我们执行了 11 次操作。

在第二个测试用例中,我们可以将 a1:=1a_1 := 1。此时 a1+a2+a3+a4+a5=1+(−1)+(−1)+1+1=1≥0a_1 + a_2 + a_3 + a_4 + a_5 = 1 + (-1) + (-1) + 1 + 1 = 1 \ge 0,且 a1⋅a2⋅a3⋅a4⋅a5=1⋅(−1)⋅(−1)⋅1⋅1=1a_1 \cdot a_2 \cdot a_3 \cdot a_4 \cdot a_5 = 1 \cdot (-1) \cdot (-1) \cdot 1 \cdot 1 = 1。因此,我们执行了 11 次操作。

在第三个测试用例中,a1+a2+a3+a4=(−1)+1+(−1)+1=0≥0a_1 + a_2 + a_3 + a_4 = (-1) + 1 + (-1) + 1 = 0 \ge 0,且 a1⋅a2⋅a3⋅a4=(−1)⋅1⋅(−1)⋅1=1a_1 \cdot a_2 \cdot a_3 \cdot a_4 = (-1) \cdot 1 \cdot (-1) \cdot 1 = 1。因此,所有条件均已满足,无需执行任何操作。

在第四个测试用例中,我们可以将 a1:=1,a2:=1,a3:=1a_1 := 1, a_2 := 1, a_3 := 1。此时 a1+a2+a3=1+1+1=3≥0a_1 + a_2 + a_3 = 1 + 1 + 1 = 3 \ge 0,且 a1⋅a2⋅a3=1⋅1⋅1=1a_1 \cdot a_2 \cdot a_3 = 1 \cdot 1 \cdot 1 = 1。因此,我们执行了 33 次操作。

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

首页