E 序列
题目描述
给定一个长度为 n (1 \le n \le 100)n(1≤n≤100) 的序列 aa,所有元素均为 11 或 -1−1。我们称 aa 是一个好序列,当且仅当同时满足以下两个条件:
a_1 + a_2 + ... + a_n \geq 0a
1
+a
2
+...+a
n
≥0;
a_1 \cdot a_2 \cdot...\cdot a_n = 1a
1
⋅a
2
⋅...⋅a
n
=1。
你可以对序列进行若干次修改,每次修改可以把序列中的 -1−1 改成 11 或从 11 改成 -1−1。
给定一个序列,问最少需要几次修改使它变成一个好的序列。
输入格式
Each test consists of multiple test cases. The first line contains a single integer tt ( 1 \le t \le 5001≤t≤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 \le n \le 1001≤n≤100 ) — the length of the array aa .
The second line of each test case contains nn integers a_1, a_2, \ldots, a_na
1
,a
2
,…,a
n
( a_i = \pm 1a
i
=±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
样例输入 #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 a_1 := 1a
1
:=1 . Then a_1 + a_2 + a_3 + a_4 = 1 + (-1) + 1 + (-1) = 0 \ge 0a
1
+a
2
+a
3
+a
4
=1+(−1)+1+(−1)=0≥0 and a_1 \cdot a_2 \cdot a_3 \cdot a_4 = 1 \cdot (-1) \cdot 1 \cdot (-1) = 1a
1
⋅a
2
⋅a
3
⋅a
4
=1⋅(−1)⋅1⋅(−1)=1 . Thus, we performed 11 operation.
In the second test case, we can assign a_1 := 1a
1
:=1 . Then a_1 + a_2 + a_3 + a_4 + a_5 = 1 + (-1) + (-1) + 1 + 1 = 1 \ge 0a
1
+a
2
+a
3
+a
4
+a
5
=1+(−1)+(−1)+1+1=1≥0 and a_1 \cdot a_2 \cdot a_3 \cdot a_4 \cdot a_5 = 1 \cdot (-1) \cdot (-1) \cdot 1 \cdot 1 = 1a
1
⋅a
2
⋅a
3
⋅a
4
⋅a
5
=1⋅(−1)⋅(−1)⋅1⋅1=1 . Thus, we performed 11 operation.
In the third test case, a_1 + a_2 + a_3 + a_4 = (-1) + 1 + (-1) + 1 = 0 \ge 0a
1
+a
2
+a
3
+a
4
=(−1)+1+(−1)+1=0≥0 and a_1 \cdot a_2 \cdot a_3 \cdot a_4 = (-1) \cdot 1 \cdot (-1) \cdot 1 = 1a
1
⋅a
2
⋅a
3
⋅a
4
=(−1)⋅1⋅(−1)⋅1=1 . Thus, all conditions are already satisfied and no operations are needed.
In the fourth test case, we can assign the values a_1 := 1, a_2 := 1, a_3 := 1a
1
:=1,a
2
:=1,a
3
:=1 . Then a_1 + a_2 + a_3 = 1 + 1 + 1 = 3 \ge 0a
1
+a
2
+a
3
=1+1+1=3≥0 and a_1 \cdot a_2 \cdot a_3 = 1 \cdot 1 \cdot 1 = 1a
1
⋅a
2
⋅a
3
=1⋅1⋅1=1 . Thus, we performed 33 operations.