AtCoder Grand Contest 045 - Problem-Solving Report
A - Xor Battle
Feels kind of idiotic.
For 0, we just need the final answer to be 0. So we maintain things from back to front: maintain the linear basis formed by combining the values at the \(0\) positions, and each time we reach a \(1\) position, just query it in there. If it isn't found, then for \(1\), the later \(0\)s aren't enough to cancel that operation, so we can declare \(1\) the winner, and vice versa.
// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 220;
typedef long long ll;
int T, n;
ll base[MAX_N], ai[MAX_N];
char str[MAX_N];
void insert(ll x)
{
for (int i = 62; i >= 0; i--)
if (x & (1LL << i))
{
if (base[i] == 0)
{
base[i] ^= x;
break;
}
x ^= base[i];
}
}
bool query(ll x)
{
for (int i = 62; i >= 0; i--)
if (x & (1LL << i))
x ^= base[i];
return x == 0;
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &ai[i]);
scanf("%s", str + 1);
for (int i = 0; i <= 62; i++)
base[i] = 0;
bool flag = false;
for (int i = n; i >= 1; i--)
{
if (str[i] == '0')
insert(ai[i]);
else if (!query(ai[i]))
{
flag = true;
break;
}
}
puts(flag ? "1" : "0");
}
return 0;
}
B - 01 Unbalanced
Why do I feel this problem is also really stupid, and yet I just couldn't figure it out... F***.
Essentially, we want a way to assign values to the ?s that minimizes the range of the prefix sums. Then we can use the standard trick of enumerating the minimum of the whole prefix sum and making adjustments on top of that minimum. Enumerate \(M\) as the minimum; then we can use a suffix minimum to check whether adjusting a ? would push a later minimum element out of bounds, that is, below the minimum. This is \(\Theta(n^2)\).
But we can see this: what we're computing is a range, so our minimum doesn't have to be very small, and it turns out that values near the global minimum already do the job. (The range floats: let the global minimum be \(X\); going down from \(X - 2\), there will be \(-1\)s or \(1\)s that can't help shrink the range.)
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 200;
int n, pre[MAX_N], suf[MAX_N], ans = 0x7fffffff;
char str[MAX_N];
void prepare()
{
for (int i = 1; i <= n; i++)
pre[i] = pre[i - 1] + (str[i] == '0' ? -1 : 1);
suf[n + 1] = MAX_N;
for (int i = n; i >= 0; i--)
suf[i] = min(suf[i + 1], pre[i]);
}
void solve(int lbound)
{
int bias = 0, upper = 0;
for (int i = 1; i <= n; i++)
{
if (str[i] == '?' && suf[i] - 2 - bias >= lbound)
bias += 2;
pre[i] -= bias, upper = max(upper, pre[i]);
}
ans = min(ans, upper - lbound);
}
int main()
{
scanf("%s", str + 1), n = strlen(str + 1);
prepare(), solve(suf[0]);
prepare(), solve(suf[0] - 1);
printf("%d\n", ans);
return 0;
}