codeforces-scrape

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

200.md (6853B)


      1 Source: https://codeforces.com/blog/entry/4769
      2 
      3 [200A — Cinema](https://codeforces.com/contest/200/problem/A)
      4 
      5 In this problem were given the field, which size is *n* × *m*, and *k* queries. Each query is the cell of the field. You had to find closest free cell for given one (there was used manhattan metric). Than found cell was marked as used. The time complexity of the solution is supposed to be ![](https://espresso.codeforces.com/1a61675d42920415a81ebf5a81ed0dee2736562a.png). Firstly, we show the main idea, than we show why solution has time complexity ![](https://espresso.codeforces.com/1a61675d42920415a81ebf5a81ed0dee2736562a.png).
      6 
      7 First of all, if *n* > *m*, than rotate matrix by 90 degrees (than we explain purpose of this transformation). We will have two matrices, size of each is *n* × *m*. In this matrices we will maintain for each cell the nearest free one to the left and to the right. Suppose we have query — cell (*x*, *y*). Iterate *d* — how much rows we will go above or below. Suppose we fix *d*, than look at the row *x* - *d*, for example (also we should do same things for row *x* + *d*). Lets find in this row nearest free cells to the left and to the right of (*x* - *d*, *y*) by using our matrices and try to update answer. When *d* will be greater than current found answer we will stop, because answer won't update in this case. After we find the answer, we have to change value in some cells of our matrices. It can be done by using structure DSU for each row.
      8 
      9 Lets show why this solution has time complexity ![](https://espresso.codeforces.com/1a61675d42920415a81ebf5a81ed0dee2736562a.png). Suppose all queries are equal, for example each query is (*x*, *y*). Than if field is big enough, all points will be placed in form of square, which is rotated by 45 degrees and have center in point (*x*, *y*). Also the side of the square will have length ![](https://espresso.codeforces.com/67ed7492d1862bd303c4da8ab21d28b87843329f.png). Than diagonal will have length ![](https://espresso.codeforces.com/1e1251fdd442e43afd61891578ea00a4edd790aa.png) too. It means that we see ![](https://espresso.codeforces.com/c220d508a81a2550050e8b6523d6ba84cb6dcba1.png) rows in each query and we do *O*(1) operations in each row. If the square doesn't fit into the field, than field is too thin verticaly or horizontaly. So we will have figure looks like rectangle, and one side of this rectangle will be less than ![](https://espresso.codeforces.com/67ed7492d1862bd303c4da8ab21d28b87843329f.png). In this case rotate field so, than least side of rectangle means rows. Than we will see not greater than ![](https://espresso.codeforces.com/67ed7492d1862bd303c4da8ab21d28b87843329f.png) rows in each query and will perform not greater than *O*(1) operations in each row.
     10 
     11 This problem is supposed to be the hardest problem of the contest.
     12 
     13 [200B — Drinks](https://codeforces.com/contest/200/problem/B)
     14 
     15 This problem was the easiest problem of the contest. There you had to find average of the given numbers. The most participants solved this problem.
     16 
     17 [200C — Football Championship](https://codeforces.com/contest/200/problem/C)
     18 
     19 In this problem was given description of the group stage of some football competition and scoring system. There were given results of all matches, excepting one, and you had to find result of the last match, satisfied some given criterias. Also Berland's team must be first or the second team of the group after than match.
     20 
     21 Lets note, that in each finished match were not greater than 18 goals. It means that we can brute-force all results of the last match, when score is not greater than 200 goals, and find the best one. One of the easiest way is to fill table to the end (it means to change points value and balls value), than to sort teams according to the given rules and to check that Berland is the first or the second team of the group.
     22 
     23 [200D — Programming Language](https://codeforces.com/contest/200/problem/D)
     24 
     25 In this task were given the list of template functions. Each function have its name and the list of types of arguments (also it can be used universal type). Also there were given set of variables and thier types, and some queries. Each query is function, which has name and list of arguments. For each query you had to find, how many functions from the given list fit to the function from query. There fit means that functions have the same name, same number of arguments and types of all arguments also equal.
     26 
     27 For solving this problem it is needed to implement comparing of functions. Constrains gave the possibility to brute-force function from the given list and check if the names and arguments of functions are equal.
     28 
     29 [200E — Tractor College](https://codeforces.com/contest/200/problem/E)
     30 
     31 In this problem were given four integer numbers *c*3, *c*4, *c*5, *s*. You had to find 0 ≤ *k*3 ≤ *k*4 ≤ *k*5 such, that *c*3·*k*3 + *c*4·*k*4 + *c*5·*k*5 = *s* and |*c*3·*k*3–*c*4·*k*4| + |*c*4·*k*4–*c*5·*k*5| is minimal.
     32 
     33 Firstly, brute-force *k*4 so, that *s*–*c*4·*k*4 ≥ 0. Than look at 4 cases, according to the sign of the value in each modulus.
     34 
     35 Lets see the case, when *c*3·*k*3–*c*4·*k*4 ≥ 0 and *c*4·*k*4–*c*5·*k*5 ≥ 0. Than we have to minimize *c*3·*k*3–*c*5·*k*5. Also 0 ≤ *k*3 ≤ *k*4 ≤ *k*5 and *c*3·*k*3 + *c*5·*k*5 = *s*–*c*4·*k*4. Lets see diofant equation *c*3·*k*3 + *c*5... *k*5 = *s*–*c*4·*k*4. It can be that this equation doesn't have solution. Lets see the case, when equation has solution. As *c*3, *c*5 ≥ 0, than for minimization *c*3·*k*3–*c*5·*k*5 we have to minimize *k*3 and maximize *k*5. All solutions of diofant equation *c*3·*k*3 + *c*5·*k*5 = *s*–*c*4·*k*4 can be described by using one argument *k*. Than we have to find such segment, that for all *k* from it, *k*3 will fit above constrains, such segment, that for all *k* from it, *k*5 will fit above constrains, find intersection of this segments and, if intersection isn't empty, choose such *k*, that *k*5 is maximal.
     36 
     37 Similar you have to manage remain 3 cases and choose optimal values *k*3 and *k*5 for fixed *k*4. Also you can note, that in all cases minimized function is linear and in segment it has minimal value in one of its ends. So we can only find such segments, that for all *k* from that segments *k*3, *k*5 will fit above constrains, and calculate answer in the ends of this segments. If for all fixed *k*4 diofant equation doesn't have solution, or intersections of the described segments are empty, than answer is *IMPOSSIBLE*, else we should find the best. So the time complexity is *O*(*s*·*log*(*s*)) — brute-force of *k*4 and solving diofant equation for fixed *k*4.