· Olympiad in Informatics

Yali CSP-S 2019 Mock Contest - Expression Tree

The idea behind this problem is really clever.

Notice that the only operators are \(+, -, \times\), so the result along a path can be expressed as a linear combination: \(ax + b\). We record the prefixes of the linear combination from the root downward and from the bottom up to the root, and then we consider using the prefixes to split the path: it is the same kind of standard trick as difference on trees.

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

· Olympiad in Informatics

Nowcoder CSP-S Senior Division Pre-Contest Training Camp 2 - Problem-Solving Report

I have to reflect carefully on this contest. The state of mind I had over the summer break must not happen again.

Suppose we already have plenty of servers. Now we consider things day by day and allocate the servers: suppose there is a set of server IDs \(\{id_i\}\), and consider allocating several of them each day, one day at a time. This guarantees that no server's allocation contains the same date twice.

That way everything can be allocated, and we directly get \(\sum a_i \leq |id_i| m\). After that it's just idiotic code.

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

· Olympiad in Informatics

[CEOI2011] Traffic — Solution

We find that this is really a multi-source coverage problem. Note the guarantee that “edges do not intersect anywhere other than at junctions”; roughly drawing a picture shows that what we cover this way is an interval (this is a bit like a problem from some year that had to do with farming).

Without this property, we could only use a bitset to maintain the set of reachable vertices, and doing it on the DAG after SCC condensation takes \(\Theta(\frac{n^2}{32})\) time. With this property, however, we can directly record the upper and lower endpoints and subtract them to get the answer.

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

· Olympiad in Informatics

P4320: Meeting on the Road — Solution

We notice that this problem is really just asking for the number of cut vertices between pairs of vertices in an undirected graph: we can be sure the number of cut vertices is unique, because if there are more cut vertices, then there are no fewer cut vertices that keep the vertex-biconnected components connected.

Now that we know roughly what the problem says and asks, we can see this: if we shrink all the non-cut vertices in the region where these cut vertices are into a single vertex, and then have the cut vertices connect to them separately, then by the “uniqueness” of “there are no fewer cut vertices that keep the vertex-biconnected components connected”, we know that the thing built this way must be a tree.

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

· Olympiad in Informatics

[Codeforces 510D] Fox And Jumping — Solution

To cover the entire line, we only need these step lengths to be able to combine into a \(1\). Then by Bézout's theorem, we have \(ax + by = \gcd(a, b)\), so we can use this to do a DP: keep combining into new \(\gcd\)s to find the minimum cost.

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

· Olympiad in Informatics

P3423: [POI2005] BAN-Bank Notes — Solution

First do a binary decomposition of the items, then run a 0/1 knapsack DP, and along the way use a bitset to record whether each transition was taken. That’s all.

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

· Olympiad in Informatics

Codeforce 708C: Centroids — Solution

We notice that this problem is really just about deciding whether, when we take, within the subtree, the largest subtree not exceeding \(\frac{n}{2}\) and attach it to the root, the subtree can still stay \(> \frac{n}{2}\).

We do one DP following the approach of the first pass, recording the largest and second-largest values of the current subtree.

Then consider how to get the Up DP values: first transition from the parent, then use the maximum and the second-smallest value (they cannot be in the same subtree, and that is the whole point of the second-smallest value) to update the current Up DP value.

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

· Olympiad in Informatics

Nowcoder CSP-S Senior Division Pre-Contest Training Camp 1 - Problem-Solving Report

A heh-heh problem. After drawing a few diagrams I found that only the \(1\) case is a first-player win, so just consider swapping the first and second player over multiple rounds.

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

· Olympiad in Informatics

Codeforces 1247D: Power Products — Solution

It really is just goddamn hacky heuristics.

Let us consider under what circumstances a number can be called \(x^k\): naturally, when the exponents of all its prime factors are multiples of \(k\), that is, \(b_i \equiv 0 \pmod k\). So we can factorize a number and write it as powers of its prime factors, except that the exponents are taken mod \(k\). Then we hash it and put it into unordered_map. Make two hashes: one of the number itself, and one of what it takes to make it a complete \(k\)-th power.

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

· Olympiad in Informatics

P3615: Toilet Plan — Solution

I really should think harder more often; I might well have been able to come up with it.

Consider taking suffix sums over this sequence, with a woman counting as -1 and a man as +1, and you find that it is enough to keep the suffix sum always greater than \(-2\). So we can maintain the suffix sum of each segment, and then maintain the maximum suffix sum.

When computing the answer, we need to know how much to raise the polyline so that it never touches the line at \(-2\); just find the largest amount of raising (that way everything is valid).

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