AtCoder Regular Contest 098 - Problem-Solving Report
C - Attention
Just mess around a bit with prefixes and suffixes and you're done.
// ARC098A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5 + 200;
int pre[2][MAX_N], n;
char str[MAX_N];
int main()
{
scanf("%d%s", &n, str + 1);
for (int i = 1; i <= n; i++)
pre[str[i] == 'E'][i]++, pre[0][i] += pre[0][i - 1], pre[1][i] += pre[1][i - 1];
int ans = 1e9;
for (int i = 1; i <= n; i++)
ans = min(pre[0][i - 1] + pre[1][n] - pre[1][i], ans);
printf("%d\n", ans);
return 0;
}
D - Xor Sum 2
Consider a property: only when \(a \& b=0\) can \(a \oplus b = a + b\) hold. Binary search or two-pointers will do.
// ARC098B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 200;
int n, ai[MAX_N], bits[20][MAX_N];
long long ans;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &ai[i]);
for (int i = 1; i <= n; i++)
for (int j = 0; j < 20; j++)
bits[j][i] = bits[j][i - 1] + ((ai[i] >> j) & 1);
for (int i = 1; i <= n; i++)
{
int l = 1, r = i, res;
while (l <= r)
{
int mid = (l + r) >> 1;
bool flag = true;
for (int j = 0; j < 20; j++)
flag &= bits[j][i] - bits[j][mid - 1] <= 1;
if (flag)
r = mid - 1, res = mid;
else
l = mid + 1;
}
ans += i - res + 1;
}
printf("%lld\n", ans);
return 0;
}
E - Range Minimum Queries
Enumerate the minimum value \(x\), then split the whole segment into several pieces whose minimum is at least \(x\). Take the top \(len - k + 1\) numbers out of each piece, sort them, and take the \(Q\)-th largest one to verify.
// ARC098C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2020;
typedef pair<int, int> pii;
int n, k, q, ai[MAX_N], ans = 1e9;
int check(int minimum_val)
{
vector<int> block, pool;
for (int i = 1; i <= n + 1; i++)
if (ai[i] >= minimum_val)
block.push_back(ai[i]);
else
{
if (block.size() >= k)
{
sort(block.begin(), block.end());
int len = block.size();
for (int i = 0; i <= len - k; i++)
pool.push_back(block[i]);
}
block.clear();
}
sort(pool.begin(), pool.end());
if (pool.size() < q)
return 1e9;
return pool[q - 1] - minimum_val;
}
int main()
{
scanf("%d%d%d", &n, &k, &q);
for (int i = 1; i <= n; i++)
scanf("%d", &ai[i]);
for (int i = 1; i <= n; i++)
ans = min(ans, check(ai[i]));
printf("%d\n", ans);
return 0;
}
F - Donation
When I first started thinking about it, I could roughly tell that this thing would end up turning into a problem on a tree. Consider that every change in the amount of money makes some vertices disappear, which produces cut vertices, and the final shape must also be a tree. Add one property: a donation only happens on the last visit, and we can determine that the final shape must be a tree.
Consider defining \(\max(a_i - b_i, 0)\) as the extra amount each vertex needs; in the final tree's shape, \(c_i\) must grow as the depth increases. We can build this tree with a DSU, then use DP to compute the maximum reserve.
// ARC098F.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 200;
typedef long long ll;
int n, m, head[MAX_N], current, ai[MAX_N], bi[MAX_N], ci[MAX_N], id[MAX_N], rk[MAX_N], mem[MAX_N];
ll dp[MAX_N], tot[MAX_N];
vector<int> G[MAX_N];
struct edge
{
int to, nxt;
} edges[MAX_N << 1];
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
int find(int x) { return mem[x] == x ? x : mem[x] = find(mem[x]); }
void dfs(int u, int fa)
{
tot[u] = bi[u];
for (auto son : G[u])
dfs(son, u), tot[u] += tot[son];
}
void calc(int u, int fa)
{
if (G[u].empty())
return (void)(dp[u] = bi[u] + ci[u]);
dp[u] = 1e18;
for (auto v : G[u])
calc(v, u), dp[u] = min(dp[u], tot[u] - tot[v] + max(1LL * ci[u], dp[v]));
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d%d", &ai[i], &bi[i]), ci[i] = max(ai[i] - bi[i], 0), id[i] = i, mem[i] = i;
sort(id + 1, id + 1 + n, [](const int &rhs1, const int &rhs2) { return ci[rhs1] < ci[rhs2]; });
for (int i = 1; i <= n; i++)
rk[id[i]] = i;
for (int i = 1, u, v; i <= m; i++)
scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
for (int idx = 1; idx <= n; idx++)
{
int u = id[idx];
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (u != find(edges[i].to) && rk[u] > rk[find(edges[i].to)])
G[u].push_back(find(edges[i].to)), mem[find(edges[i].to)] = u;
}
dfs(id[n], 0), calc(id[n], 0), printf("%lld\n", dp[id[n]]);
return 0;
}