Logically speaking, doing the second part directly would take \(\Theta(n^4)\) time, and it doesn't look like there's much to work with either. Consider computing all the bordering positions \((x, y, c_a, c_y)\), meaning that the point with color \(c_x\) and index \(x\) borders the point with color \(c_y\) and index \(y\). There are only \(\Theta(n^2)\) such bordering positions, so we just sort them into groups and then handle them with a DSU.
Continue reading →Chinese original · English translation by Claude Opus 5.5
A fairly template-ish problem on network flow with lower and upper bounds. First consider the case \(red > blue\): we color all the points red and then just do a maximum replacement. We consider making a node for each point, each x-coordinate and each y-coordinate. The edges between the x-coordinates and the source, and between the y-coordinates and the sink, need a bit of rough calculation, which gives the flow range \(\frac{cnt_x}{2} \leq flow \leq \frac{d + cnt_x}{2}\). Once we have this range, we just plug it into the template code.
Continue reading →Chinese original · English translation by Claude Opus 5.5
First of all, the \(\Theta(n^3)\) for this problem is easy to write. If you want to get the \(\Theta(n^2)\) score, we can observe that the DP process is really just a process of taking suffix mins, so that's the optimization done (of course you could also do linear programming, but that's hard to write).
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem is pretty interesting, and it has some weird details.
First, considering only the forward strings, we just stuff them straight into an Aho–Corasick automaton and do the standard-trick DP. Now consider a string lying in the second half: it turns out that as long as you characterize the first half properly, the second half gets characterized automatically, so we take each string's reverse, complement its characters, and stuff it into the forward strings' Aho–Corasick automaton, and that lets the string appear in the second half.
Continue reading →Chinese original · English translation by Claude Opus 5.5
First, if there were no constraints, pulling nodes out starting from the leaves would actually always give a solution; you can think of an out-tree rooted at the final exit to understand this logic. Now there are \(m\) directed edges. A first thought is to try each node as the last node and then check whether the new graph has a cycle, which is \(\Theta(n^2)\).
Continue reading →Chinese original · English translation by Claude Opus 5.5
If the string isn't a palindrome to begin with, the answer is \(1\); if it is, consider finding a split point so that neither the left nor the right part is one, and then the answer is \(2\); if that still can't be found, there is no solution, because in the end the string is either a plain string or something that turns into a plain string after one deletion.
Continue reading →Chinese original · English translation by Claude Opus 5.5
The first thing to think of is removing all the contained intervals, and then we focus on how to do the DP. We can consider letting \(dp[i][j]\) be the optimal answer for the first \(i\) intervals with \(j\) of them deleted. Normally, the “choose \(j\) intervals” problems we do are fairly simple, but \(k \leq 200\) makes this one a real pain.
Continue reading →Chinese original · English translation by Claude Opus 5.5