Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
A product is strictly positive only if no element is zero and the number of negative elements is even.
Every 0 must be changed. The cheapest way is always 0 -> 1, costing exactly operation per zero.
The only elements that affect the sign are the -1s. Count how many there are.
If the number of -1s is even, after fixing all zeros, the product is already positive.
If the number of -1s is odd, one -1 must become positive. Since operations only increase values, that costs -1 -> 0 -> 1, i.e. extra operations.
Let:
-1 values.A product is positive when two things are true:
Zeros are annoying because if even one zero exists, the whole product becomes , which is not strictly positive. So every 0 must be changed. Since we can only increase values, the cheapest fix is:
That costs operation per zero, so fixing zeros costs operations.
Now look at the -1s.
If is even, then the product of all negative values is positive. After turning all zeros into ones, the entire product is positive. Answer:
If is odd, the product is negative after fixing zeros. We need to flip one -1 into a positive number. But one operation only gives:
That still makes the whole product zero, which is still not positive. So we need one more operation:
That costs operations. Answer:
So the whole problem is just counting. Tiny math bonk, no DP dragon hiding under the bed.
Algorithm
For each test case:
-1s.-1s is odd, add .Correctness Proof
We prove the algorithm outputs the minimum number of operations.
First, every zero must be changed, because any array containing a zero has product , which is not strictly positive. Since changing a zero to a nonzero value requires at least one increment, at least operations are necessary. The algorithm performs exactly these operations by changing each zero to .
After all zeros are fixed, the sign of the product depends only on the number of -1 elements. If this count is even, the product is already positive, so no more operations are needed. Thus the algorithm is optimal in this case.
If the number of -1 elements is odd, the product is negative after fixing zeros. To make it positive, the parity of the number of negative elements must change. Since operations only increase values, the cheapest way to remove one negative factor is to change one -1 into 1, which takes exactly two operations: -1 -> 0 -> 1. One operation is not enough because -1 -> 0 leaves a zero in the array, making the product . Therefore at least extra operations are necessary, and the algorithm performs exactly extra operations.
In all cases, the algorithm reaches a strictly positive product using the minimum possible number of operations. Therefore, it is correct.
Complexity
For each test case, we scan the array once.
Time complexity:
Memory complexity:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int zeros = 0, negs = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == 0) zeros++;
if (x == -1) negs++;
}
int ans = zeros;
if (negs % 2 == 1) ans += 2;
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int zeros = 0, negs = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == 0) zeros++;
if (x == -1) negs++;
}
int ans = zeros;
if (negs % 2 == 1) ans += 2;
cout << ans << '\n';
}
return 0;
}