CF1834A.Unit Array

普及/提高-

通过率:0%

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++an0a_1 + a_2 + \ldots + a_n \ge 0 ;
  • a1a2an=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.

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt ( 1t5001 \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 ( 1n1001 \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 .

输出格式

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

输入输出样例

  • 输入#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)=00a_1 + a_2 + a_3 + a_4 = 1 + (-1) + 1 + (-1) = 0 \ge 0 and a1a2a3a4=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=10a_1 + a_2 + a_3 + a_4 + a_5 = 1 + (-1) + (-1) + 1 + 1 = 1 \ge 0 and a1a2a3a4a5=1(1)(1)11=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=00a_1 + a_2 + a_3 + a_4 = (-1) + 1 + (-1) + 1 = 0 \ge 0 and a1a2a3a4=(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=30a_1 + a_2 + a_3 = 1 + 1 + 1 = 3 \ge 0 and a1a2a3=111=1a_1 \cdot a_2 \cdot a_3 = 1 \cdot 1 \cdot 1 = 1 . Thus, we performed 33 operations.

首页