· Olympiad in Informatics

[FZOI] Round #33 (Div. 0) - Problem-Solving Report

In the exam I only managed to derive \(f(x) = \mu(x) * \sigma_0^2(x) = \prod_{i = 1}^m (2c_i + 1)\), and obviously min_25 could just insta-kill it directly (but I forgot the template code, boo-hoo-hoo-hoo-hoo)

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

Codeforces 1354E: Graph Coloring — Solution

This problem is fairly easy.

Notice that after placing a \(2\) on the spanning tree, the remaining vertices at an even distance from that vertex are all determined. So for a connected component, there are only two ways to color it with \(2\).

Then we can run a knapsack over these connected components, and use it to check whether there is a coloring with \(n_2\).

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

LibreOJ#3049. [Twelve-Province Joint Contest 2019] String Problem — Solution

After reading this problem, you can more or less tell that you need to build a SAM of the reversed string, then locate the \(A, B\) strings with binary lifting, add the edges, and find the longest path. That's it.

But it turns out that doing it directly like this goes GG when it runs into \(|A| < |B|\), because they may end up on the same node. In that case we sort by length and make a few extra shadow nodes for the duplicated nodes, and that's it.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P3245: [HNOI2016] Big Number — Solution

I've gone full zen.

Take \(p\) and \(10\) and find their \(\gcd\). If they are coprime, it is just counting same-color pairs in a range of the sequence \(a_i = \text{the number from i to n} \bmod p\), which Mo's algorithm handles; if it is \(2\), color the last digits \(0, 2, 4, 6, 8\) and count same-color pairs, which can be done in \(\Theta(n)\); if it is \(5\), just color \(0, 5\).

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

Codeforces 739E: Gosha is hunting — Solution

At a glance you can see a \(\Theta(n^3)\) DP. Actually, with a double WQS binary search you can get it done in \(\Theta(n \log^2 n)\) time, but I didn't write it that way.

Let's consider the contribution when something is chosen. \(p_a, p_b\) are the individual contributions; consider the contribution when both are chosen at once, \(1 - (1 - p_a)(1 - p_b) = p_a + p_b - p_a p_b\). So we can consider max-cost max-flow to compute this expression.

Note that we need to make sure the cost is positive every time, because we don't actually need the max flow, only the relative max cost.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P4338: [ZJOI2018] History — Solution

ZJOI problems are all such high quality! They really are all great problems.

Actually, this covering thing is equivalent to the number of light/heavy chain switches during Access on an LCT. Let's consider the static problem: for a node \(u\), if its subtree contains several descendants that come from different children's subtrees, then we need to allocate them sensibly to push this number of light/heavy chain switches to the maximum.

Rephrased, it is the maximum number of pairs formed between points of pairwise different colors. Let \(S_0 = a_u, S_i = [\text{total number of accesses in the } i \text{-th subtree}]\). Then the answer is actually \(\max\{ \sum_{i = 0}^m S_i - 1, 2(\sum_{i = 0}^m S_i - \max_{i \in [0, m]} S_i) \}\): the former is when we don't take anything into account, the latter is connecting to one child. The condition for the latter to be the maximum is really that it becomes the heavy child. Let's consider maintaining this thing.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P5327: [ZJOI2019] Language — Solution

Great problem! Great problem!

First, this problem requires knowing one property: the set of countries that each point can reach and trade with forms a connected spanning subgraph. So what we ultimately need to compute is the size of the set corresponding to each country.

How do we compute this quickly? We need to take out every one of the \(m\) paths that passes over the current point \(u\), and then compute the intersection of these paths. This is one way to compute it, but it really isn't fast.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P4428: [BJOI2018] Binary — Solution

Print a table and you find that a valid interval must satisfy one of the following conditions:

  • If the number of \(1\)s in the interval is odd, then there must be at least \(2\) \(0\)s.
  • The number of \(1\)s in the interval is even.

We can consider maintaining the invalid intervals, because the features of invalid intervals are a bit more obvious; there is only this kind:

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P3349: [ZJOI2016] Little Stars — Solution

How do I have no clue at all? (I'm so weak) But to be fair, the problem really is pretty good.

One approach close to the intended solution is to set up a DP: let \(dp[u][i][stat]\) be the state where we are currently at node \(u\), it has label \(i\), and the set of labels already used is \(stat\), and then do a tree DP. It is not hard to work out that the complexity of this thing is \(\Theta(n^3 3^n)\).

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P4425: [HNOI/AHOI2018] Turntable — Solution

This is the first problem I've done that uses a segment tree to maintain a monotonic stack, so let me write a blog post about it.

First, what this problem asks for is just \(ans = \min_{i \in [n, 2n)} \max_{j \in [i - n + 1, i]} a_j + i - j\); rewriting it a bit gives \(ans = (\min_{i \in [1, n)} \max_{j \in [i, i + n - 1]} a_j + i - j) + n - 1\).

We can consider computing \(B_i = a_i - i\) and then taking the \(\min\). However, the complexity of this is still high. When we consider the static problem, we find that this problem can actually be done in \(\Theta(n)\), because all it really takes is a reversed (right-to-left) monotonic stack.

Continue reading →Chinese original · English translation by Claude Opus 5.5