A. Be Positive

time limit per test

1 second

memory limit per test

256 megabytes

Given an array a of n elements, where each element is equal to −1, 0, or 1. In one operation, you can choose an index i and increase ai by 1 (that is, assign ai:=ai+1). Operations can be performed any number of times, choosing any indices.

The goal is to make the product of all elements in the array strictly positive with the minimum number of operations, that is, a1⋅a2⋅a3⋅...⋅an>0. Find the minimum number of operations.

It is guaranteed that this is always possible.

Input

Each test consists of several test cases.

The first line contains one integer t (1≤t≤104) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains one integer n (1≤n≤8) --- the length of the array a.

The second line contains n integers a1,a2,...,an, where −1≤ai≤1 --- the elements of the array a.

Output

For each test case, output one integer --- the minimum number of operations required to make the product of the elements in the array strictly positive.

Example

Input

Copy

复制代码

3

3

-1 0 1

4

-1 -1 0 1

5

-1 -1 -1 0 0

Output

Copy

复制代码

3

1

4

Note

In the first test case: from [−1,0,1], you can obtain [1,1,1] in 3 operations.

In the second test case: it is enough to perform 0→1 (1 operation). In the resulting array a=[−1,−1,1,1], the product of all elements is 1.

In the third test case: turning two zeros into ones (2 operations), and one −1 into 1 (another 2 operations), for a total of 4.

解题说明:水题,分别统计出-1、0的次数,然后判断即可,0肯定需要变成1,-1如果出现奇数次肯定需要变成1。

cpp 复制代码
#include<stdio.h>

void main() 
{
	int t;
	scanf("%d", &t);
	while (t--) 
	{
		int c = 0, k = 0;
		int a;
		scanf("%d", &a);
		int b[9];
		for (int i = 0; i < a; i++) 
		{
			scanf("%d", &b[i]);
		}
		for (int i = 0; i < a; i++)
		{
			if (b[i] == 0)
			{
				c = c + 1;
			}
			if (b[i] < 0)
			{
				k = k + 1;
			}
		}
		if (k % 2 != 0)
		{
			printf("%d\n", c + 2);
		}
		else
		{
			printf("%d\n", c);
		}
	}
	return 0;
}
相关推荐
kupeThinkPoem12 分钟前
跳表有哪些算法?
数据结构·算法
前端小L21 分钟前
图论专题(二十一):并查集的“工程应用”——拔线重连,修复「连通网络」
数据结构·算法·深度优先·图论·宽度优先
88号技师32 分钟前
2025年9月一区SCI-孤行尺蠖觅食优化算法Solitary Inchworm Foraging-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法
前端小L1 小时前
图论专题(二十五):最小生成树(MST)——用最少的钱,连通整个世界「连接所有点的最小费用」
算法·矩阵·深度优先·图论·宽度优先
前端小L1 小时前
图论专题(二十三):并查集的“数据清洗”——解决复杂的「账户合并」
数据结构·算法·安全·深度优先·图论
CoovallyAIHub1 小时前
破局红外小目标检测:异常感知Anomaly-Aware YOLO以“俭”驭“繁”
深度学习·算法·计算机视觉
点云SLAM2 小时前
图论中邻接矩阵和邻接表详解
算法·图论·slam·邻接表·邻接矩阵·最大团·稠密图
啊董dong2 小时前
课后作业-2025年11月23号作业
数据结构·c++·算法·深度优先·noi
星释2 小时前
Rust 练习册 80:Grains与位运算
大数据·算法·rust
zzzsde2 小时前
【C++】C++11(1):右值引用和移动语义
开发语言·c++·算法