[Miscellaneous Problems] - September 19, 2019
Picking Numbers from a Grid
One look at the complexity: it is on the order of \(O(nm)\), so consider a DP with two loops. After analyzing the whole statement, we find that we cross a boundary if and only if the other connected component has a larger weight than the current one; otherwise we won't give up the current gold just to cross the boundary. So we do something like a DP: since each cell can only be walked through once, in each column you can only choose to keep going up or keep going down, so this DP has no aftereffects, and then handling the connection checks between connected components finishes it.
// B.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1030;
int mp[MAX_N][MAX_N], n, m, T;
ll dp[MAX_N][MAX_N];
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &mp[i][j]);
for (int j = 1; j <= m; j++)
{
ll cur = -1e18;
for (int i = 1; i <= n; i++)
if (mp[i][j] == -1)
cur = -1e18, dp[i][j] = cur;
else
cur = max(cur, dp[i][j - 1]) + mp[i][j], dp[i][j] = cur;
cur = -1e18;
for (int i = n; i >= 1; i--)
if (mp[i][j] == -1)
cur = -1e18;
else
cur = max(cur, dp[i][j - 1]) + mp[i][j], dp[i][j] = max(cur, dp[i][j]);
int down_bound = 0;
for (int i = n; i >= 1; i--)
if (mp[i][j] == -1)
break;
else if (dp[i][j - 1] >= 0)
{
down_bound = i;
break;
}
cur = 0;
for (int i = 1; i < down_bound; i++)
if (mp[i][j] == -1)
break;
else
cur += mp[i][j], dp[i][j] = max(dp[i][j], cur);
int up_bound = n + 1;
for (int i = 1; i <= n; i++)
if (mp[i][j] == -1)
break;
else if (dp[i][j - 1] >= 0)
{
up_bound = i;
break;
}
cur = 0;
for (int i = n; i > up_bound; i--)
if (mp[i][j] == -1)
break;
else
cur += mp[i][j], dp[i][j] = max(dp[i][j], cur);
}
ll ans = -1;
for (int i = 1; i <= n; i++)
ans = max(ans, dp[i][m]);
printf("%lld\n", ans);
}
return 0;
}
Coloring
It really is a goddamn sign-in problem.
First, we can enumerate that there are \(i\) of \(A\), then check whether the rest is divisible by \(B\). If it is, it can be added to the answer. Consider computing this part's contribution: out of \(n\) bricks choose \(i\) to be the \(A\) ones, then multiply by choosing, out of the \(n\) bricks, \(\frac{n - iA}{B}\) of them; the overlapping ones count as \(C\), and the non-overlapping ones are \(B\).
// A.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 3e5 + 200, mod = 998244353;
int fac[MAX_N], fac_inv[MAX_N], T;
ll n, A, B, x;
int quick_pow(int bas, int tim)
{
int ans = 1;
while (tim)
{
if (tim & 1)
ans = 1LL * ans * bas % mod;
bas = 1LL * bas * bas % mod;
tim >>= 1;
}
return ans;
}
int combinator(int n_, int k_)
{
if (n_ < k_ || n_ < 0 || k_ < 0)
return 0;
return 1LL * fac[n_] * fac_inv[k_] % mod * fac_inv[n_ - k_] % mod;
}
int main()
{
scanf("%d", &T);
for (int i = fac[0] = 1; i < MAX_N; i++)
fac[i] = 1LL * fac[i - 1] * i % mod;
fac_inv[MAX_N - 1] = quick_pow(fac[MAX_N - 1], mod - 2);
for (int i = MAX_N - 2; i >= 0; i--)
fac_inv[i] = 1LL * fac_inv[i + 1] * (i + 1) % mod;
while (T--)
{
scanf("%lld%lld%lld%lld", &n, &A, &B, &x);
ll res = 0, ans = 0;
for (int i = 0; i <= n; i++)
{
ll tmp = x - 1LL * i * A;
if (tmp > 0 && (tmp % B == 0))
ans = (1LL * ans + 1LL * combinator(n, i) * combinator(n, tmp / B) % mod) % mod;
}
printf("%lld\n", ans);
}
return 0;
}
String
This problem is actually fairly easy to figure out. Think about the recurrence for counting subsequences:
\[ dp[i] = 2dp[i - 1] - dp[lastPos[str[i]]] \]
First compute the number of subsequences of the prefix, then consider constructing onward: we find that the DP array is monotonically increasing, so each time we transition we just take the smallest \(lastPos[char]\).
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_K = 27, mod = 1e9 + 7;
int dp[MAX_K], n, m, k, prev_pos[MAX_K], ans;
int main()
{
scanf("%d %d\n", &m, &k);
int ans = 1, x, old;
char ch;
while (true)
{
ch = getchar(), x = ch - 'a' + 1;
if (x < 1 || x > 26)
break;
old = ans;
ans = 2LL * ans % mod, ans = (1LL * ans + mod - dp[x]) % mod;
dp[x] = old, prev_pos[x] = ++n;
}
for (int i = 1; i <= m; i++)
{
x = 0;
for (int j = 1; j <= k; j++)
if (!x || prev_pos[x] > prev_pos[j])
x = j;
prev_pos[x] = ++n, old = ans;
ans = 2LL * ans % mod, ans = (ans + mod - dp[x]) % mod;
dp[x] = old;
}
printf("%d", ans);
return 0;
}
Worship-Fang Club
Stupid pseudotree: record the values on the cycle and the values on the chains, and then you can happily get AC.
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200100;
int head[MAX_N], current, deg[MAX_N], n, val[MAX_N], deletion[MAX_N], mem[MAX_N], sum[MAX_N], loop[MAX_N];
struct edge
{
int to, nxt;
} edges[MAX_N << 1];
int find(int x) { return x == mem[x] ? x : mem[x] = find(mem[x]); }
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
void toposort()
{
queue<int> q;
for (int i = 1; i <= n; i++)
if (deg[i] == 0)
q.push(i);
while (!q.empty())
{
int u = q.front();
q.pop(), deg[u]--;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (--deg[edges[i].to] == 0)
q.push(edges[i].to);
}
for (int i = 1; i <= n; i++)
if (deg[i] > 0)
loop[find(i)] += val[i];
}
void dfs(int u, int fat)
{
if (deg[fat] <= 0)
val[u] += val[fat];
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fat && deg[edges[i].to] <= 0)
dfs(edges[i].to, u);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &val[i]), mem[i] = i, sum[i] = val[i];
for (int i = 1, v; i <= n; i++)
{
scanf("%d", &v), addpath(i, v), addpath(v, i), deg[v]++;
int x = find(i), y = find(v);
if (x != y)
mem[y] = x, sum[x] += sum[y];
}
toposort();
for (int i = 1; i <= n; i++)
if (deg[i] > 0)
dfs(i, 0);
for (int i = 1; i <= n; i++)
if (deg[i] > 0)
printf("%d\n", loop[find(i)]);
else
printf("%d\n", val[i] + loop[find(i)]);
return 0;
}
Ranking
Why can it be this simple?
// A.cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, prefix, suffix;
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1, p; i <= n; i++)
scanf("%d", &p), prefix += p - 1, suffix += m - p;
printf("%d\n%d\n", max(1, m - suffix), min(m, prefix + 1));
return 0;
}
Or Perhaps It Is Too Perfect
This problem is one that combines data structures with thinking. What it asks: how to arrange the \(q\) operations so that the final string differs from the target string as little as possible.
First, set the answer to the number of \(0\) in the target string, assuming the whole sequence is completely covered with \(1\). The answer we want is this answer cut down as far as possible (that is, the minimum). So, for each operation, we put a mark on its left endpoint and on its right endpoint to record how much it can be reduced by at the least, and then the problem is solved (a bit like the minimum cut method).
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 800010, INF = 0x3f3f3f3f;
int min_val[MAX_N], seq[MAX_N], n, q, ans;
vector<int> queries[MAX_N];
#define mid ((l + r) >> 1)
#define lson (p << 1)
#define rson ((p << 1) | 1)
void pushup(int p)
{
min_val[p] = min(min_val[lson], min_val[rson]);
}
void build(int l, int r, int p)
{
if (l == r)
return (void)(min_val[p] = ((l == 0) ? 0 : INF));
build(l, mid, lson), build(mid + 1, r, rson);
pushup(p);
}
void update(int qx, int l, int r, int p, int val)
{
if (l == r)
return (void)(min_val[p] = min(min_val[p], val));
if (qx <= mid)
update(qx, l, mid, lson, val);
else
update(qx, mid + 1, r, rson, val);
pushup(p);
}
int query(int ql, int qr, int l, int r, int p)
{
if (ql <= l && r <= qr)
return min_val[p];
int ret = INF;
if (ql <= mid)
ret = min(ret, query(ql, qr, l, mid, lson));
if (mid < qr)
ret = min(ret, query(ql, qr, mid + 1, r, rson));
return ret;
}
#undef mid
#undef lson
#undef rson
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &seq[i]), ans += !seq[i];
scanf("%d", &q);
for (int i = 1, x, y; i <= q; i++)
scanf("%d%d", &x, &y), queries[x - 1].push_back(y);
build(0, n, 1);
for (int i = 0; i < n; i++)
{
for (int j = 0, siz = queries[i].size(); j < siz; j++)
update(queries[i][j], 0, n, 1, query(i, queries[i][j], 0, n, 1));
update(i + 1, 0, n, 1, query(i, i, 0, n, 1) + (seq[i] ? 1 : -1));
}
printf("%d\n", ans + query(n, n, 0, n, 1));
return 0;
}
For the Many, Many Death Anniversaries
First compute the maximum independent set on the tree, then work it out: if even twice the independent set is less than \(n\), then the policeman can build more houses that cannot catch fire; if the independent set is greater than \(k\), then cutting edges won't work either;
// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5050;
int head[MAX_N], current, n, k, dp[MAX_N][2];
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++;
}
void dfs(int u, int fa)
{
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
dfs(edges[i].to, u), dp[u][0] += max(dp[edges[i].to][1], dp[edges[i].to][0]);
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
dp[u][1] = max(dp[u][1], dp[u][0] - max(dp[edges[i].to][1], dp[edges[i].to][0]) + dp[edges[i].to][0] + 1);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &k);
for (int i = 1, u, v; i <= n - 1; i++)
scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
dfs(1, 0);
int independent_set = max(dp[1][0], dp[1][1]);
if ((independent_set << 1) < n || k < independent_set - 1)
puts("policeman");
else
puts("arsonist");
return 0;
}