CF1834A.Unit Array
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Given an array a of length n, which elements are equal to −1 and 1. Let's call the array a good if the following conditions are held at the same time:
-
a1+a2+…+an≥0;
-
a1⋅a2⋅…⋅an=1.
In one operation, you can select an arbitrary element of the array ai and change its value to the opposite. In other words, if ai=−1, you can assign the value to ai:=1, and if ai=1, then assign the value to ai:=−1.
Determine the minimum number of operations you need to perform to make the array a good. It can be shown that this is always possible.
给定一个长度为 n 的数组 a,其元素均为 −1 或 1。若数组 a 同时满足以下两个条件,则称其为“好”数组:
-
a1+a2+…+an≥0;
-
a1⋅a2⋅…⋅an=1。
在一次操作中,你可以任选数组中的一个元素 ai,将其值变为相反数。即:若 ai=−1,则令 ai:=1;若 ai=1,则令 ai:=−1。
求使数组 a 变为“好”数组所需的最少操作次数。可以证明,该目标总能达成。
输入格式
Each test consists of multiple test cases. The first line contains a single integer t (1≤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 n (1≤n≤100) — the length of the array a.
The second line of each test case contains n integers a1,a2,…,an (ai=±1) — the elements of the array a.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤500),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤100),表示数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(其中 ai=±1),表示数组 a 的元素。
输出格式
For each test case, output a single integer — the minimum number of operations that need to be done to make the a array good.
对于每个测试用例,输出一个整数——使数组 a 变为“好”的所需最少操作次数。
输入输出样例
输入#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:=1. Then a1+a2+a3+a4=1+(−1)+1+(−1)=0≥0 and a1⋅a2⋅a3⋅a4=1⋅(−1)⋅1⋅(−1)=1. Thus, we performed 1 operation.
In the second test case, we can assign a1:=1. Then a1+a2+a3+a4+a5=1+(−1)+(−1)+1+1=1≥0 and a1⋅a2⋅a3⋅a4⋅a5=1⋅(−1)⋅(−1)⋅1⋅1=1. Thus, we performed 1 operation.
In the third test case, a1+a2+a3+a4=(−1)+1+(−1)+1=0≥0 and a1⋅a2⋅a3⋅a4=(−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 a1:=1,a2:=1,a3:=1. Then a1+a2+a3=1+1+1=3≥0 and a1⋅a2⋅a3=1⋅1⋅1=1. Thus, we performed 3 operations.
在第一个测试用例中,我们可以将 a1:=1。此时 a1+a2+a3+a4=1+(−1)+1+(−1)=0≥0,且 a1⋅a2⋅a3⋅a4=1⋅(−1)⋅1⋅(−1)=1。因此,我们执行了 1 次操作。
在第二个测试用例中,我们可以将 a1:=1。此时 a1+a2+a3+a4+a5=1+(−1)+(−1)+1+1=1≥0,且 a1⋅a2⋅a3⋅a4⋅a5=1⋅(−1)⋅(−1)⋅1⋅1=1。因此,我们执行了 1 次操作。
在第三个测试用例中,a1+a2+a3+a4=(−1)+1+(−1)+1=0≥0,且 a1⋅a2⋅a3⋅a4=(−1)⋅1⋅(−1)⋅1=1。因此,所有条件均已满足,无需执行任何操作。
在第四个测试用例中,我们可以将 a1:=1,a2:=1,a3:=1。此时 a1+a2+a3=1+1+1=3≥0,且 a1⋅a2⋅a3=1⋅1⋅1=1。因此,我们执行了 3 次操作。
输入解题思路,AI测评打分。不知道怎么写?