350.md (6122B)
1 Source: https://codeforces.com/blog/entry/9042 2 3 ## [350A - TL](/contest/350/problem/A "Codeforces Round 203 (Div. 2)") 4 5 Let's *v* = *min*(*a**i*), *p* = *max*(*a**i*), *c* = *min*(*b**i*). So, if *max*(2 * *v*, *p*) < *c*, then answer is *max*(2 * *v*, *p*), else answer is - 1. 6 7 Author solution: [4632352](/contest/350/submission/4632352 "Submission 4632352 by gridnevvvit") 8 9 ## [350B - Resort](/contest/350/problem/B "Codeforces Round 203 (Div. 2)") 10 11 Input data represents a graph, by using a array of parents of every vertex. Because every vertex has at most one parent, we can use following solution: we will go up to parent of vertex *v* (*prev*[*v*]) until not found vertex with the outcome degree ≥ 2. It is better to calculate outcome degrees in advance. After all, we will update the answer. 12 13 This algorithm works in *O*(*n*). 14 15 Author solution: [4632399](/contest/350/submission/4632399 "Submission 4632399 by gridnevvvit") 16 17 ## [350C - Bombs](/contest/350/problem/C "Codeforces Round 203 (Div. 2)") 18 19 First of all, Let's sort all point by increasing of value |*x**i*| + |*y**i*|, all points we will process by using this order. We will process each point greedily, by using maximum six moves. Now we want to come to the point (*x*, *y*). Let's *x* ≠ 0. Then we need to move exactly |*x*| in the *dir* direction (if *x* < 0 the dir is *L*, *x* > 0 — *R*). Similarly we will work with *y*-coordinates of point (*x*, *y*). Now we at the point (*x*, *y*), let's pick a bomb at point (*x*, *y*). After that we should come back to point (0, 0). Why it is correct to sort all point by increasing of [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry)? If you will look at the path that we have received, you can notice that all points of path have lower Manhattan distance, i.e. we will process this points earlier. 20 21 This solution works in  22 23 Authors solution: [4632478](/contest/350/submission/4632478 "Submission 4632478 by gridnevvvit") 24 25 ## [350D - Looking for Owls](/contest/350/problem/D "Codeforces Round 203 (Div. 2)") 26 27 It's possible to solve this problem by using only integer calculations. Normalization of the line *Ax* + *By* + *C* is following operation: we multiply our equation on the value , where *g* = *gcd*(*A*, *gcd*(*B*, *C*)), if *A* < 0 (*orA* = 0*andB* < 0) then *sgn* equals to - 1, else sgn equals to 1. 28 29 Now the solution. We will have two maps (map<> in С++, TreeMap(HashMap) in Java) to a set of points (it's possible that some points will have multiply occurrence into the set). In first map we will store right boundaries of the segments, in second — left boundaries (in increasing order). 30 31 In advance for every segment we will build a normalized line, and for this normalized line we will put in our maps left and right segments of the segment. 32 33 After all, for every fixed line let's sort our sets. 34 35 Let's fix two different circles. After that, let's check that distance beetween them is greater then sum their radiuses, also you should check that circles has same radius. We can assume that we builded a line between centers of circles (*x*1, *y*1) and (*x*2, *y*2). Perpendicular to this line will have next coefficients (center of the segment [(*x*1, *y*1), (*x*2, *y*2)] also will belong to the next line) *A* = 2(*x*1 - *x*2), *B* = 2(*y*1 - *y*2), *C* = - ((*x*1 - *x*2) * (*x*1 + *x*2) + (*y*1 - *y*2) * (*y*1 + *y*2)). After that you need to calculate values *cntL*, *cntR* by using binary search on set of points that lie on this line. *cntL* — amount of left boundaries that lie on the right side of point ((*x*1 + *x*2) / 2, (*y*1 + *y*2) / 2), *cntR* -- amount of right boundaries that lie on the left side of the point ((*x*1 + *x*2) / 2, (*y*1 + *y*2) / 2). After that you should add to answer value *cntV* - *cntR* - *cntL*,l where *cntV* — amount of segments, that lie on the nolmalized line. 36 37 Total complexity: . 38 39 solution: [4632546](/contest/350/submission/4632546 "Submission 4632546 by gridnevvvit") 40 41 ## [350E - Wrong Floyd](/contest/350/problem/E "Codeforces Round 203 (Div. 2)") 42 43 Let's do the following: construct the graph with the maximum possible number of edges and then remove the excess. First of all, you can notice that if *k* = *n* answer is - 1. Else let's fix some marked vertex, for example *a*1. Let's put in our graph all edges except edges beetween *a*1 and *x*, where *x* — an another marked vertex. 44 45 So, why this algorithm is correct? If Valera's algorithm is wrong, then there are a ''bad'' pair of vertexes (i, j). ``Bad'' pair is a pair for that Valera's algorithm works wrong. So, there are not marked vertex *v* on the shortest path from i to j, and *v* ≠ *i*, and *v* ≠ *j*. Without loss of generality, we can assume, that distance beetween *i* and *j* equals to 2, but Valera's algorithm gives greater answer. There are some cases, that depends on the type of vertexes *i*, *j*. But we can look only at the case where (*i*, *j*) are marked vertexes. First, add to the graph all edges beetween not fixed (*i*, *j*) vertexes. Second, add to the graph edges beetween some fixed vertex (*i* or *j*) and some not marked vertex. Third, add to the graph edges beetween *i* and some marked vertex *v*, where *v* ≠ *j*. It's simple to understand, that if we add some another edge, the Valera's algorithm will work correctly. Total amount of edges is . 46 47 **BONUS** Simple bonus. For same contrains (*n*, *m*, *k*) can you build a graph, where Valera's code works correctly? 48 49 Код: [4632600](/contest/350/submission/4632600 "Submission 4632600 by gridnevvvit")