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.
Stop thinking about the exact arrays first. Think about each district's total population . Once is fixed, the winner just needs a strict majority.
For any district with total population , the winning party needs at least voters. That one formula is basically the whole problem wearing a fake mustache.
Let and . If , it is instantly impossible. Otherwise, the only question is whether can lie inside the possible range of total A-voters.
If the string has both 0 and 1, surplus voters are easy to hide: extra A-voters can go into a 0 district, and extra B-voters can go into a 1 district. So you only need enough A-voters for all 0 districts and enough B-voters for all 1 districts.
The annoying case is when all districts have the same winner. Start with population everywhere. Each even gives one "free" extra voter without increasing the winner's required count; after those freebies, every 2 extra voters force 1 more winner voter, rounded up.
The useful move is to forget the individual arrays for a minute and look at district totals.
Let
, and .
If , we do not even have enough voters to satisfy the minimum district populations, so the answer is NO. No politics, no geometry, just arithmetic being rude.
For a district with total population , the winning party must have a strict majority, so it needs at least
voters.
That is the core fact.
When both winner types exist
Suppose there is at least one 0 district and at least one 1 district.
For every 0 district, party A must win. If district has minimum population , then A needs at least
voters there. So define
.
Similarly, every 1 district must be won by party B, so define
.
These are clearly necessary:
0 districts;1 districts.They are also sufficient, as long as .
Why? Because once both district types exist, surplus voters are harmless:
0 district, where more A only helps;1 district, where more B only helps.So for mixed strings, the answer is exactly:
, , and .
Equivalently, possible A totals form the interval
.
When all districts have the same winner
Now suppose all districts are 0, so A must win everywhere. The all-1 case is symmetric with A and B swapped.
Here, the false assumption is: "extra voters are always harmless." Nope. If everyone must be in an A-winning district, then adding lots of voters can force us to add more A-voters too. You cannot hide a mountain of B-voters in a B-winning district because there are no B-winning districts. Sad.
We need the minimum possible number of A-voters among all districts, given total population .
Start with district having exactly voters. The initial required A count is
.
There are
extra voters to place.
Now look at what happens when we increase a district's total population by 1.
If is even, say , then initially A needs . With one extra voter, the total becomes , and A still only needs . So this first extra voter is free.
If is odd, say , then initially A needs . With one extra voter, the total becomes , and A needs . So that extra voter costs one more A-voter.
Therefore:
Let be the number of even .
If , then the minimum winner count stays .
Otherwise, after using free extras, there are extras left, and they increase the minimum winner count by
.
So the minimum required winner voters is
.
For all 0, we need
.
For all 1, we need the same condition for B:
.
Why intervals are enough
For a fixed district total :
0 district, A can be any integer from to ;1 district, A can be any integer from to .So each district contributes an integer interval of possible A-votes. The sum of integer intervals is still an integer interval. There are no sneaky parity gaps here. The strict majority condition already handles the parity.
That means checking the minimum and maximum possible total A-voters is enough.
Complexity
Each test case is processed in time and extra memory. Across all tests, this is , which easily fits.
#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 tc;
cin >> tc;
while (tc--) {
int n;
ll x, y;
cin >> n >> x >> y;
string s;
cin >> s;
int cnt0 = 0, cnt1 = 0;
for (char c : s) {
if (c == '0') cnt0++;
else cnt1++;
}
ll totalMin = 0;
ll need0 = 0, need1 = 0;
ll allNeed = 0;
ll evenCnt = 0;
for (int i = 0; i < n; i++) {
ll p;
cin >> p;
totalMin += p;
ll need = p / 2 + 1;
allNeed += need;
if (p % 2 == 0) evenCnt++;
if (s[i] == '0') need0 += need;
else need1 += need;
}
ll total = x + y;
bool ok = false;
if (total >= totalMin) {
if (cnt0 > 0 && cnt1 > 0) {
ok = (x >= need0 && y >= need1);
} else {
ll extra = total - totalMin;
ll minWinner = allNeed;
if (extra > evenCnt) {
minWinner += (extra - evenCnt + 1) / 2;
}
if (cnt0 == n) ok = (x >= minWinner);
else ok = (y >= minWinner);
}
}
cout << (ok ? "YES\n" : "NO\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 tc;
cin >> tc;
while (tc--) {
int n;
ll x, y;
cin >> n >> x >> y;
string s;
cin >> s;
int cnt0 = 0, cnt1 = 0;
for (char c : s) {
if (c == '0') cnt0++;
else cnt1++;
}
ll totalMin = 0;
ll need0 = 0, need1 = 0;
ll allNeed = 0;
ll evenCnt = 0;
for (int i = 0; i < n; i++) {
ll p;
cin >> p;
totalMin += p;
ll need = p / 2 + 1;
allNeed += need;
if (p % 2 == 0) evenCnt++;
if (s[i] == '0') need0 += need;
else need1 += need;
}
ll total = x + y;
bool ok = false;
if (total >= totalMin) {
if (cnt0 > 0 && cnt1 > 0) {
ok = (x >= need0 && y >= need1);
} else {
ll extra = total - totalMin;
ll minWinner = allNeed;
if (extra > evenCnt) {
minWinner += (extra - evenCnt + 1) / 2;
}
if (cnt0 == n) ok = (x >= minWinner);
else ok = (y >= minWinner);
}
}
cout << (ok ? "YES\n" : "NO\n");
}
}