Editorial | CF521 / Codeforces Round 295 (Div. 1)
Editorial for a virtual-contest run of Codeforces Round 295 (Div. 1), covering the approach and solution for problems A through E.
Machine-translated from the Chinese original.
Problems not solved independently
- 521E
A
Problem
The alphabet is . Given a string of length , count the number of ways to construct a string such that, over all cyclic shifts, the total number of matching positions between and is maximized.
Solution
Not hard to see that, because of the shifting, each character of contributes the count of matching characters in . The total number of matches is maximized exactly when every character of is one of the characters with the highest occurrence count in . So it suffices to count the occurrences of each character in ; if characters tie for the highest count, the answer is (each position of can independently pick any of these characters).
Takeaway
A bit of observation.
B
Problem
V and P built a shape from blocks, numbered . Set up a Cartesian coordinate system with the ground as the -axis and straight up as the positive -axis. Each block’s position is given by the coordinates of its bottom-left corner, and every block’s coordinates are integers.
Solution
As stated, since each digit of a base- number has place value , both players’ strategies are simple greedy. V needs to remove the largest currently removable value, and P needs to remove the smallest currently removable value. Maintain a set of currently removable blocks. Each time a block is removed, trigger a local update: check the removability of the surrounding blocks and update the set.
Takeaway
Simple to think through, brutal to implement.
C
Problem
Given an -digit decimal number , insert plus signs between the digits to form an expression. Find the sum, over all ways of inserting them, of the expression’s value.
Solution
Consider the contribution of each to the answer, which depends on the position of the nearest after .
- If the is right after , then itself contributes , and the remaining s can be placed in ways, contributing
- If the is right after , then itself contributes , and the remaining s can be placed in ways, contributing
- If the is right after , then itself contributes , and the remaining s can be placed in ways, contributing
And so on. Note that when is the same distance from the , the multiplied binomial coefficient is the same, so they can be computed together. This gives
D
Problem
Given positive integers .
There are operations, each given by three parameters , , and a positive integer .
- : set to
- : add to
- : multiply by
You may choose at most of these operations to execute, and you may choose the execution order, with the goal of maximizing .
Solution
First, once the chosen operations are fixed, the execution order should always be assignments first, then additions, then multiplications.
Since a chosen assignment operation can never make smaller, an assignment can be converted into an addition. Then note that, for the same , all additions should be executed greedily from largest to smallest, so additions can in turn be converted into multiplications. Finally, sorting the resulting multiplications tells you which operations should be executed.
Takeaway
Simple greedy.
E
Problem
Given a simple undirected graph with vertices and edges, determine whether there exist two vertices with at least three pairwise vertex-disjoint simple paths between them.
Solution
This is the graph for samples ; the red edges form its spanning tree.


Observation: if two cycles share an edge, then there exist two vertices satisfying the requirement, since we can take the endpoints of the shared segment as , giving three paths. From the spanning-tree perspective, an answer exists whenever some tree edge is covered by two non-tree edges.
Now DFS each connected component once. For every non-tree edge , brute-force mark the tree edges it covers. If a tree edge was already marked by a non-tree edge , the answer can be obtained directly from and .

Fix , , with an ancestor of . Let ; the three paths are , , and . Finding the paths can be implemented by brute-force walking up parent pointers.
Takeaway
A constructive problem.