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 useful operation on a substring of length removes characters and costs . Since the string must lose exactly characters total, the cost is almost fixed.
If a valid sequence uses useful operations, then
So minimize the number of operations, not the raw costs.
Define the balance of a string as
A substring can be collapsed into 1 iff its balance is nonnegative, and into 0 iff its balance is nonpositive.
If the string has at least one 1, then operations always suffice: keep that 1, collapse the part before it, collapse the part after it, then merge through the middle. So we only need to classify whether the answer uses or operations.
Check these in order: already 1; total balance ; a positive-balance prefix/suffix or total balance ; a nonnegative-balance prefix/suffix or adjacent 11; otherwise it needs operations. Then add .
Let
A substring can be replaced by 1 exactly when , and by 0 exactly when . Ties are flexible, which is the one tiny mercy this problem gives us.
First remove the cost distraction. If an operation uses a substring of length , it shrinks the string by and costs . Over the whole process, the length must drop from to , so
If we use useful operations, then
So the minimum cost is , where is the minimum number of operations. Nice little bait-and-switch.
If the string has no 1, it is impossible: every substring has a 0 majority, so no operation can ever create a 1.
Otherwise, . Pick any existing 1. Collapse the part before it into one character if it exists, and collapse the part after it into one character if it exists. Now the string has the form x1y with maybe some missing sides. Collapse the first two characters to 1, then collapse the remaining two characters to 1. Each of those last merges is valid because the substring contains a 1 and has length at most .
Now we just classify .
If the string is already exactly 1, then .
For , the only useful operation must collapse the entire string to 1, so this is possible exactly when
For , there are two ways this can happen.
If some proper prefix has positive balance, collapse the remaining suffix to one character. That new character has balance at worst , so the whole remaining string still has nonnegative balance and can be collapsed to 1. The same works for a positive-balance suffix.
Also, if , then operations are enough. If the string already has a positive prefix/suffix, we are in the previous case. Otherwise the last character cannot be 1, so it is 0; then the prefix of length has balance . Collapse that prefix to 1, getting 10, then collapse 10 to 1.
These are also the only cases. Suppose one first operation is followed by one final collapse. Let the operated substring have balance , and let its replacement have balance . The new total balance must be nonnegative.
If , then , and
Since we are past the case, , so this forces and .
If , then , and
So the part outside the operated substring has positive total balance. That outside part is a prefix, a suffix, or the sum of both; therefore at least one proper prefix or suffix has positive balance. So no other -operation case exists.
For , again there are two useful patterns.
If some proper prefix has nonnegative balance, collapse the remaining suffix to 0, collapse that prefix to 1, then merge the resulting 10 to 1. The suffix can be collapsed to 0 because after the earlier checks failed, the total balance is negative, so the suffix balance is nonpositive. Symmetric for a nonnegative suffix.
If the string contains adjacent 11, collapse everything before that pair to one character and everything after that pair to one character. The two middle ones contribute , and the two collapsed sides contribute at worst , so the final string has nonnegative balance and can be collapsed to 1. Thus adjacent 11 gives .
The remaining case really needs operations. Assume all previous checks failed. Then:
11.The last point implies every substring has balance at most : without adjacent ones, ones can outnumber zeros by at most one.
Now take any possible first operation.
If it creates 0, then for the resulting string to be solvable in two more operations, it would need either total balance or a positive prefix/suffix. Total balance would mean the outside part has balance , impossible because any nonempty outside prefix/suffix is negative. A positive prefix after inserting a 0 would need some substring from the far side with balance at least , also impossible with no adjacent 11. Same for suffixes.
If the first operation creates 1, let the operated substring have balance . If the new total balance is , then the outside balance is , and because the original total is at most , we must have . If the operation touches an end, this gives a forbidden nonnegative prefix/suffix. Otherwise both outside parts have balance , so the operated substring must start and end with 0; its inside would then have balance , impossible without adjacent 11.
Finally, if creating that 1 produced a positive prefix or suffix, then adding back the operated substring, whose balance was nonnegative, would give a nonnegative proper prefix or suffix in the original string. Forbidden again.
So no -operation solution exists in the leftover case, and since operations always suffice when a 1 exists, the classification is complete.
Algorithmically, compute:
11 appears.Then choose using the cases above and print .
The complexity is per test case, with extra memory.
#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;
string s;
cin >> n >> s;
int ones = 0;
for (char c : s) ones += (c == '1');
if (ones == 0) {
cout << -1 << '\n';
continue;
}
if (s == "1") {
cout << 0 << '\n';
continue;
}
int total = 2 * ones - n;
bool prefPos = false, prefNonNeg = false;
bool suffPos = false, suffNonNeg = false;
bool has11 = false;
int bal = 0;
for (int i = 0; i + 1 < n; i++) {
bal += (s[i] == '1' ? 1 : -1);
prefPos |= (bal > 0);
prefNonNeg |= (bal >= 0);
has11 |= (s[i] == '1' && s[i + 1] == '1');
}
bal = 0;
for (int i = n - 1; i > 0; i--) {
bal += (s[i] == '1' ? 1 : -1);
suffPos |= (bal > 0);
suffNonNeg |= (bal >= 0);
}
int k;
if (total >= 0) {
k = 1;
} else if (prefPos || suffPos || total == -1) {
k = 2;
} else if (prefNonNeg || suffNonNeg || has11) {
k = 3;
} else {
k = 4;
}
cout << (ll)n - 1 + k << '\n';
}
}#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;
string s;
cin >> n >> s;
int ones = 0;
for (char c : s) ones += (c == '1');
if (ones == 0) {
cout << -1 << '\n';
continue;
}
if (s == "1") {
cout << 0 << '\n';
continue;
}
int total = 2 * ones - n;
bool prefPos = false, prefNonNeg = false;
bool suffPos = false, suffNonNeg = false;
bool has11 = false;
int bal = 0;
for (int i = 0; i + 1 < n; i++) {
bal += (s[i] == '1' ? 1 : -1);
prefPos |= (bal > 0);
prefNonNeg |= (bal >= 0);
has11 |= (s[i] == '1' && s[i + 1] == '1');
}
bal = 0;
for (int i = n - 1; i > 0; i--) {
bal += (s[i] == '1' ? 1 : -1);
suffPos |= (bal > 0);
suffNonNeg |= (bal >= 0);
}
int k;
if (total >= 0) {
k = 1;
} else if (prefPos || suffPos || total == -1) {
k = 2;
} else if (prefNonNeg || suffNonNeg || has11) {
k = 3;
} else {
k = 4;
}
cout << (ll)n - 1 + k << '\n';
}
}