codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

196.md (8055B)


      1 Source: https://codeforces.com/blog/entry/4717
      2 
      3 [197A - Plate Game](/contest/197/problem/A "Codeforces Round 124 (Div. 2)")
      4 
      5 If first player can't make first move (table is too small and plate doesn't fit it, i.e. 2*r* > *min*(*a*, *b*)), second player wins. Else first player wins. Winning strategy for first player: place first plate to the center of table. After that he symmetrically reflects moves of second player with respect to center of table. If second player has move, first player has symmetrical move, too. If not, first player won.
      6 
      7 [197B - Limit](/contest/197/problem/B "Codeforces Round 124 (Div. 2)")
      8 
      9 From math lessons we know, that only higher degrees of polinomials matter in this problem.
     10 
     11 1. If denominator degree is larger than numenator degree, answer is "0/1".
     12 2. If numenator degree is larger, answer is infinity. But what is sign of this infinity? To get it consider signs of highest degree factors of polinomials. If they are same, answer is positive infinity, else — negative infinity.
     13 3. If degrees of numenator and denominator are equal, answer is ![](https://espresso.codeforces.com/5619182fdfdf201b5c960a318fb6de715413d8f4.png). To get irreducible fraction, you should divide this numbers by *gcd*(*a*0, *b*0). And don't forget that denominator of answer must be positive.
     14 
     15 [196A - Lexicographically Maximum Subsequence](/contest/196/problem/A "Codeforces Round 124 (Div. 1)")
     16 
     17 Solution is greedy. First, write all 'z' letters (if there is any) — answer must contain them all for sure. Now it's time for 'y' letters. We can use only those of them which are on the right of last used 'z' letter. Then write 'x' letters — they must be on the right of the last used 'y' and 'z' letters. And so on.
     18 
     19 [196B - Infinite Maze](/contest/196/problem/B "Codeforces Round 124 (Div. 1)")
     20 
     21 Answer is "Yes" iff there are two distinct, reachable from start position cells, which correspond to same cell in initial labyrinth.
     22 
     23 Proof: If these cells exist, move to first of them, and infinitely repeat moves leading from first to second. On the contrary, if infinite far path exist, on this path we obviously can find such cells.
     24 
     25 How to find out if they exist? Start DFS from initial cell. For each cell visited, let *visit*[*x*%*n*][*y*%*m*] = (*x*, *y*). Now, if DFS tries to go to cell (*x*, *y*), *visit*[*x*%*n*][*y*%*m*] contains something, and (*x*, *y*) ≠ *visit*[*x*%*n*][*y*%*m*], we found these cells: they are (*x*, *y*) and *visit*[*x*%*n*][*y*%*m*]. Notice that DFS will visit no more than *nm* + 1 cells (Dirichlet's principle). So the asymptotic is *O*(*nm*).
     26 
     27 [196C - Paint Tree](/contest/196/problem/C "Codeforces Round 124 (Div. 1)")
     28 
     29 No three points are in the same line, so the solution always exists.
     30 
     31 First, choose any one vertex as the root of tree. Find size of each subtree using dfs.
     32 
     33 Then, we can build the answer recursively.
     34 
     35 Put the root of tree to the most lower left point.
     36 
     37 Sort all other points by angle relative to this lower left point.
     38 
     39 Let us name the sizes of subtrees of the root as *s*1, *s*2, ..., *s**k*.
     40 
     41 Run the algorithm recursively, giving first *s*1 points (in sorted order) for the first subtree of root, next *s*2 points for the second subtree and so on.
     42 
     43 Obviously, no two edges from different subtrees can intersect now.
     44 
     45 At each step of recursion we are to put the root of current subtree to the first point in sorted-by-angle order, and then sort other points by angle relative to it.
     46 
     47 So, no two subtrees will have any intersecting edges.
     48 
     49 The asymptotic of solution is ![](https://espresso.codeforces.com/08a5dbb1ac845802fdbad24f72be3ff1e5b3d340.png).
     50 
     51 [196D - The Next Good String](/contest/196/problem/D "Codeforces Round 124 (Div. 1)")
     52 
     53 Notice, that only palindromes with length *d* and *d* + 1 matter. Any palindrome with greater length contains one of them. Let's call these palindromes bad.
     54 
     55 First, find leftmost position *pos*, in which we surely should increase value of symbol. If there are no bad subpalindromes, *pos* = |*s*| - 1, else *pos* is leftmost position amongst all ends of bad palindromes.
     56 
     57 Increase *s*[*pos*]. Increase it more, while *s*[*pos*] is end of bad subpalindrome. If you try increase 'z' symbol, you should proceed to increasing previous symbol. If this way you reached situation when you need to increase first symbol, and it is 'z', answer is "Impossible".
     58 
     59 Now, let pos be position of leftmost changed symbol. We know, that prefix *s*[0..*pos*] doesn't contain bad palindromes. Now we can greedily fill suffix *s*[*pos* + 1..*length*(*s*) - 1]: go over positions *i* in ascending order, assign *s*[*i*] = 'a', and increase it, while *s*[*i*] is end of bad palindrome. Obviously, any of suffix symbols will be 'a', 'b' or 'c'.
     60 
     61 So we got algorithm, which requires fast implementation of next operations — assigning single symbol, and query: is given substring palindrome? You can perform this operations using hashes and Fenwick tree.
     62 
     63 Let's learn, how to get hash of substring in dynamically changing string. If we can it, we will keep string *s* and it's reversed copy. For query of second type we just need to compare hashes of substring in *s* and hash of corresponding substring in reversed copy.
     64 
     65 Let Fenwick tree store values *h*[*i*] = *s*[*i*]*P**i*, where *P* is the prime number used for hashing. Then hash of substring *s*[*L*..*R*] equals to (*h*[*L*] + *h*[*L* + 1] + ...*h*[*R*])*P*- *L*. For assigning *s*[*i*] = *c*, add value (*c* - *s*[*i*])*P**i* to *h*[*i*]. Both these operations Fenwick tree does in ![](https://espresso.codeforces.com/e864584c28b3beb52a5d9a7cbbcda3d80a9d4515.png).
     66 
     67 Also we have faster solution without hashes and data structures, it will be published soon.
     68 
     69 [196E - Opening Portals](/contest/196/problem/E "Codeforces Round 124 (Div. 1)")
     70 
     71 First of all, we can note that if each graph vertex is portal, the answer will be a sum of all edges' weights in MST (minimal spanning tree). We can find MST by using Kruskal's algo.
     72 
     73 In this problem, not an every vertex is portal. Let's fix this.
     74 
     75 Start with a precalculation. Run Dijkstra's algo from all the portals, simultaneously. We will get *d*[*i*] — a distance between vertex *i* and *p*[*i*] — the nearest portal to vertex *i*.
     76 
     77 Let's trace Kruskal's algo on a graph of portals. On the first iteration, it will choose the edge with the minimal cost, i.e. a shortest path between all the portals in the original graph.
     78 
     79 Let the path leads from portal *x* to portal *y*. Note that there exists a path with the same length such as *p*[*i*] changes only once through it. Indeed, *p*[*x*] = *x*, *p*[*y*] = *y*, i.e. *p*[*i*] changed on the path. If it happens on edge ![](https://espresso.codeforces.com/c59da485d3a4e463f0157335ace916b7c377dcd8.png), *p*[*i*] = *x*, a path ![](https://espresso.codeforces.com/a97c0e87443330fae5983a268b019ee0d7b03a9c.png) will not be longer than the path from *x* to *y*.
     80 
     81 As *p*[*i*] = *x* and *p*[*i*] = *y*, we can see that the length of this path will be *d*[*i*] + *w*(*i*, *j*) + *d*[*j*], where *w*(*i*, *j*) is the weight of edge (*i*, *j*). Kruskal's algo will add this value to the answer and merge portals *x* and *y*. The shortest-path trees of vertexes *x* and *y* will also be merged.
     82 
     83 Note, that almost nothing changed. The next edge for Kruskal's algo can be find in a similar way — ![](https://espresso.codeforces.com/9fbdc4078a8b3f752e24c4371823ba7df8e584d3.png). If this edge connects *x* and *y* again, DSU makes us not to count this edge, otherwise this edge connects a different pair of edges and will be counted in an answer.
     84 
     85 We can easily implement this. Just create a new graph of portals, with an edge (*p*[*i*], *p*[*j*]) of weight *d*[*i*] + *w*(*i*, *j*) + *d*[*j*] for every edge (*i*, *j*) of weight *w*(*i*, *j*) from original graph and run Kruskal's algo.
     86 
     87 Finally, note that if the starting vertex is not a portal, we shall add *d*[1] to the answer.