Skip to content

4 min read中文

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 {A,T,C,G}\{A,T,C,G\}. Given a string SS of length nn, count the number of ways to construct a string TT such that, over all cyclic shifts, the total number of matching positions between SS and TT is maximized.

Solution

Not hard to see that, because of the shifting, each character of TT contributes the count of matching characters in SS. The total number of matches is maximized exactly when every character of TT is one of the characters with the highest occurrence count in SS. So it suffices to count the occurrences of each character in SS; if xx characters tie for the highest count, the answer is xnx^n (each position of TT can independently pick any of these xx characters).

Takeaway

A bit of observation.

B

Problem

V and P built a shape from mm blocks, numbered 0m10\sim m-1. Set up a Cartesian coordinate system with the ground as the xx-axis and straight up as the positive yy-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-mm number has place value mm, 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 5×55\times 5 blocks and update the set.

Takeaway

Simple to think through, brutal to implement.

C

Problem

Given an nn-digit decimal number a1a2...ana_1a_2...a_n, insert kk 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 aia_i to the answer, which depends on the position of the nearest ++ after aia_i.

  • If the ++ is right after aia_i, then aia_i itself contributes 100×ai10^0\times a_i, and the remaining ++s can be placed in (n2k1)\binom {n-2} {k-1} ways, contributing 100×ai×(n2k1)10^0\times a_i\times\binom {n-2} {k-1}
  • If the ++ is right after ai+1a_{i+1}, then aia_i itself contributes 101×ai10^1\times a_i, and the remaining ++s can be placed in (n3k1)\binom {n-3} {k-1} ways, contributing 101×ai×(n3k1)10^1\times a_i\times\binom {n-3} {k-1}
  • If the ++ is right after ai+2a_{i+2}, then aia_i itself contributes 102×ai10^2\times a_i, and the remaining ++s can be placed in (n4k1)\binom {n-4} {k-1} ways, contributing 102×ai×(n4k1)10^2\times a_i\times\binom {n-4} {k-1}

And so on. Note that when aia_i is the same distance from the ++, the multiplied binomial coefficient is the same, so they can be computed together. This gives

i=1nk10i1×(j=1niaj×(n1ik1)+ani+1×(nik))\sum_{i=1}^{n-k}10^{i-1}\times (\sum_{j=1}^{n-i}a_j\times \binom{n-1-i}{k-1}+a_{n-i+1}\times\binom{n-i}{k})

D

Problem

Given kk positive integers a1,a2,...,aka_1,a_2,...,a_k.

There are nn operations, each given by three parameters tt, ii, and a positive integer bb.

  • t=1t=1: set aia_i to bb
  • t=2t=2: add bb to aia_i
  • t=3t=3: multiply aia_i by bb

You may choose at most mm of these nn operations to execute, and you may choose the execution order, with the goal of maximizing i=1kai\prod_{i=1}^{k} a_i.

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 aia_i smaller, an assignment can be converted into an addition. Then note that, for the same ii, 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 nn vertices and mm 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 1,21,2; the red edges form its spanning tree.

18f227ee32f4cc1dc8e27cae7344f8d989b9f9cc407d5a4c938495b90c4485a8.jpg

56c3ac4a9a28df775875ec857621a6cfbeec97a793de03064e3e34e61df38cb3.jpg

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 u,vu,v, 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 (a,b)(a,b), brute-force mark the tree edges it covers. If a tree edge was already marked by a non-tree edge (c,d)(c,d), the answer can be obtained directly from (a,b)(a,b) and (c,d)(c,d).

Screenshot 2020-11-11 171828.jpg

Fix depb>depadep_b\gt dep_a, depd>depcdep_d\gt dep_c, with dd an ancestor of bb. Let e=LCA(a,c)e=\operatorname{LCA}(a,c); the three paths are ded\rightarrow e, dbaed\rightarrow b\rightarrow a\rightarrow e, and dced\rightarrow c\rightarrow e. Finding the paths can be implemented by brute-force walking up parent pointers.

Takeaway

A constructive problem.