codeforces-scrape

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

600.md (5830B)


      1 Source: https://codeforces.com/blog/entry/21827
      2 
      3 [600A - Extract Numbers](/contest/600/problem/A "Educational Codeforces Round 2")
      4 
      5 This is a technical problem. You should do exactly what is written in problem statement.
      6 
      7 [600B - Queries about less or equal elements](/contest/600/problem/B "Educational Codeforces Round 2")
      8 
      9 Let's sort all numbers in a. Now let's iterate over elements of *b* and for element *b**j* find the index of lowest number that is greater than *b**j*. We can do that using binary search. That index will be the answer for value *b**j*.
     10 
     11 Complexity: *O*(*nlogn*).
     12 
     13 [600C - Make Palindrome](/contest/600/problem/C "Educational Codeforces Round 2")
     14 
     15 Let's denote *cnt**c* — the number of occurences of symbol *c*. Let's consider odd values *cnt**c*. Palindrome can not contain more than one symbol *c* with odd *cnt**c*. Let's denote symbols with odd *cnt**c* as *a*1, *a*2...*a**k* (in alphabetical order). Let's replace any one of symbols *a**k* with symbol *a*1, *a**k* - 1 with *a*2 and so on until the middle of *a*. Now we have no more than one odd symbol. If we have some let's place it in the middle of the answer. First half of answer will contain ![](https://espresso.codeforces.com/79312a737de8b4f7530de91e7e9608dfaaf56ae5.png) occurences of symbol *c* in alphabetical order. The second half will contain the same symbols in reverse order. For example for string *s* = *aabcd* at first we will replace *d* by
     16 
     17 Unable to parse markup [type=CF_TEX]
     18 
     19 in the middle and after permuting the symbols we got *abcba*. Easy to see that it's the optimal solution.
     20 
     21 Compexity: *O*(*n*).
     22 
     23 [600D - Area of Two Circles' Intersection](/contest/600/problem/D "Educational Codeforces Round 2")
     24 
     25 If the circles don't intersect than the answer is 0. We can check that case with only integer calculations (simply by comparing the square of distance between centers with square of the sum of radiuses). If one of the circles is fully in other then the answer is the square of the smaller one. We can check this case also with only integer calculations (simply by comparing the square of distance between centers with square of the difference of radiuses).
     26 
     27 So now let's consider the general case. The answer will be equal to the sum of two circular segments. Let's consider the triangle with apexes in centers if circles and in some intersecting point of the circles. In that triangle we know all three sides so we can compute the angle of the circular segment. So we can compute the square of circular sector. And the only thing that we should do now is to subtract the square of triangle with apexes in the center of circle and in the intersecting points of circles. We can do that by computing the half of absolute value of cross product. So we have the following formulas:
     28 
     29 ![](https://espresso.codeforces.com/a9a3e3b108b9499ec5138c7773b2a12132cd60c3.png),
     30 
     31 where *d* is the distance between centers of the circles. And also we should do the same thing with second circle by replacing of indices 1 ≤ *ftrightarrow*2.
     32 
     33 ![ ](/predownloaded/8b/88/8b886fc0f6fe7f731828ebb352fc2c8efb890976.jpg)
     34 
     35 Complexity: *O*(1).
     36 
     37 [600E - Lomsat gelral](/contest/600/problem/E "Educational Codeforces Round 2")
     38 
     39 The name of this problem is anagram for ''Small to large''. There is a reason for that :-) The author solution for this problem uses the classic technique for computing sets in tree. The simple solution is the following: let's find for each vertex *v* the ''map<int, int>'' — the number of occurences for each colour, ''set<pair<int, int>>'' — pairs the number of occurences and the colour, and the number *sum* — the sum of most frequent colours in subtree of *v*. To find that firstly we should find the same thing for all childs of *v* and then merge them to one. These solution is correct but too slow (it works in *O*(*n*2*logn*) time). Let's improve that solution: every time when we want to merge two *map*-s *a* and *b* let's merge the smaller one to larger simply by iterating over all elements of the smaller one (this is the ``Small to large''). Let's consider some vertex *v*: every time when vertex *v* will be moved from one *map* to another the size of the new map will be at least two times larger. So each vertex can be moved not over than *logn* times. Each moving can be done in *O*(*logn*) time. If we accumulate that values by all vertices then we get the complexity *O*(*nlog*2*n*).
     40 
     41 I saw the solutions that differs from author's but this technique can be used in a lot of other problems.
     42 
     43 [600F - Edge coloring of bipartite graph](/contest/600/problem/F "Educational Codeforces Round 2")
     44 
     45 Let's denote *d* is the maximum degree of vertex in graph. Let's prove that the answer is *d*. We will build the constructive algorithm for that (it will be the solution to problem). Let's colour the edges one by one in some order. Let (*x*, *y*) be the current edge. If there exist colour *c* that is free in vertex *x* and in vertex *y* then we can simply colour (*x*, *y*) with *c*. If there is no such colour then there are a couple of colours *c*1, *c*2 so that *c*1 is in *x* and not in *y* and *c*2 is in *y* but not in *x*. Let's make vertex *y* free from colour *c*2. Denote *z* the other end of edge from *y* with colour *c*2. If *z* is free from colour *c*1 then we can colour *x*, *y* with *c*2 and recolour *y*, *z* with *c*1. So me make alternation. If *z* is not free from colour *c*1 let's denote *w* the other end of the edge from *z* with colour *c*1. If *w* is free from colour *c*2 then again we can do alternation. And so on. We will find an alternating chain because the graph is bipartite. To find the chain we can use depth first search. Each chain contains no more than *O*(*n*) vertices. So we have:
     46 
     47 Сложность решения: *O*(*nm*).