codeforces-scrape

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

370.md (6923B)


      1 Source: https://codeforces.com/blog/entry/9866
      2 
      3 ### [370A - Rook, Bishop and King](/contest/370/problem/A "Codeforces Round 217 (Div. 2)")
      4 
      5 There are two approaches to this task. The first is use BFS to find the shortest path three times. The second is to notice that:
      6 
      7 * A rook can reach the destination in one or two moves. If the starting and the destination fields are in the same row or column, one move is enough.
      8 * A bishop can reach only fields that are colored the same as the starting cell, and can do this in at most two moves: if the starting and the destination fields are on the same diagonal, one move is enough. To find this out, check that *r*1 - *c*1 = *r*2 - *c*2 OR *r*1 + *c*1 = *r*2 + *c*2.
      9 * A king should make *max*(|*r*1 - *r*2|, |*c*1 - *c*2|) moves.
     10 
     11 ```
     12     int r1, c1, r2, c2;
     13     cin >> r1 >> c1 >> r2 >> c2;
     14     if (r1 == r2 || c1 == c2) cout << 1; else cout << 2;
     15     cout << " ";
     16     if ((r1 + c1) % 2 != (r2 + c2) % 2) cout << 0; else {
     17         if (r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2) cout << 1; else cout << 2;
     18     }
     19     cout << " ";
     20     cout << max(abs(r1 - r2), abs(c1 - c2)) << endl;
     21 ```
     22 
     23 ### [370B - Berland Bingo](/contest/370/problem/B "Codeforces Round 217 (Div. 2)")
     24 
     25 It is good idea to think about cards as set of numbers. It is easy to see that card *a* can’t be finished before *b* if *b* is subset of *a*. So all you need is to find such cards (sets) which do not have other card (other set) as subset.
     26 
     27 Since there are at most 1000 cards, you may iterate through all pairs and check that one card contains other in naive way like:
     28 
     29 ```
     30 bool contains(vector<int> a, vector<int> b) // b in a?
     31 {
     32     forn(i, b.size())
     33     {
     34         bool in = false;
     35         forn(j, a.size())
     36             if (a[j] == b[i])
     37                 in = true;
     38         if (!in)
     39             return false;
     40     }
     41 
     42     return true;
     43 }
     44 ```
     45 
     46 ### [370C - Mittens](/contest/370/problem/C "Codeforces Round 217 (Div. 2)")
     47 
     48 Let’s show that if the most frequent color appears not more than ![](https://espresso.codeforces.com/832254ba69494673dbc525f2e9193b28c955acd0.png) times, than all children can get mittens of distinct colors. One way to construct such solution is to sort all left mittens in the order of decreasing frequency of their colors: for the input 1 2 1 2 3 1 3 3 1 we get 1 1 1 1 3 3 3 2 2. To obtain the sequence of right mittens, rotate the sequence of left mittens to the left by the maximum color frequency (in the example it is 4, so we get the sequence 3 3 3 2 2 1 1 1 1). Then just match the sequences (1 — 3, 1 — 3, 1 — 3, 1 — 2, 3 — 2, 3 — 1, 3 — 1, 2 — 1, 2 — 1). It can be easily shown that all pairs consist of distinct colors.
     49 
     50 OK, but what to do if there is a dominating color that appears more than half times? Use exactly the same algorithm! It will maximize the number of pairs of distinct colors.
     51 
     52 ### [370D - Broken Monitor](/contest/370/problem/D "Codeforces Round 217 (Div. 2)")
     53 
     54 There are a lot of correct approaches to solve the problem. But there are much more incorrect :)
     55 
     56 One way to solve the problem is following. It is easy to see that in possible answer there are two opposite sides each containing `w`. In opposite case frame can be shrinked. So the size of frame is *dx* or *dy*, where *dx* = *maxx* - *minx* + 1 and *dy* = *maxy* - *miny* + 1 (*minx*, *maxx*, *miny*, *maxy* are coordinates of left/right/top/bottom `w`s). Obviously, you should choose *max*(*dx*, *dy*) as a size.
     57 
     58 Now we know the size of the required frame. How to find it’s leftmost-topmost corner?
     59 
     60 The set of possible *x*s is: *minx*, *maxx* - *size* + 1 and 0. Indeed, you may move frame to the left until it will abuts to `w` by left size/right size of abuts to the left side of monitor.
     61 
     62 Similarly, the set of possible *y*s as y-coordinate of leftmost-topmost corner: *miny*, *maxy* - *size* + 1, 0.
     63 
     64 Now the solution looks like:
     65 
     66 ```
     67 find minx, maxx, miny, maxy
     68 dx = maxx-minx+1, dy=maxy-miny+1
     69 size = max(dx, dy)
     70 foreach x in {minx, maxx-size+1, 0}
     71     foreach y in {miny, maxy-size+1, 0}
     72         if frame_correct(x, y, size)
     73             print answer
     74             exit algorithm
     75 ```
     76 
     77 All you need is to write `frame_correct`. You may iterate through frame and check that all cells inside monitor and calculate the number of `w`s on it. If all the cells are inside and calculated number equals to total number of `w`, then return `true`.
     78 
     79 This solution works in *O*(*nm*).
     80 
     81 ### [370E - Summer Reading](/contest/370/problem/E "Codeforces Round 217 (Div. 2)")
     82 
     83 For each book number that is in the sequence, find the leftmost and the rightmost position of this number. In other words, for each such book number we find a segment of positions that should consist of this number. If for some pair of numbers there segments intersect, it is impossible to construct the answer. The same thing happens if some segment has length more than 5. It is reasonable to separately handle the case when all given numbers are zeroes. In this case, fill in the numbers greedily, spending 2 days on each book (probably, except the last one).
     84 
     85 So, we have some blocks of numbers and gaps between them. Lets do the following DP: each state of DP is described by two values (*i*, *j*): *i* means the number of block (lets enumerate them consecutively), *j* means how far to the right will this block eventually extend (if there is a gap after this block, it is possible that we fill some prefix of this gap with the same book number that is in the block). It is clear that *j* - *i* will not exceed 5, so we actually can describe the state by values (*i*, *j* - *i*), which may sound more convenient. So, the number of states is linear. Lets say that *D*(*i*, *j*) is true if it it possible to correctly fill all the gaps that come before the *i*-th block, under condition that the *i*-th block extends to the position *j*, and *D*(*i*, *j*) is false otherwise. To calculate the value of *D*(*i*, *j*), lets try to extend the *i*-th block to the left in all (not so many) possible ways (to replace some number of consecutive zeroes that are in the gap just before the *i*-th block). Then, try to fix where the previous block can actually end (fix the state *D*(*i* - 1, *k*), where *D*(*i* - 1, *k*) is true, of course). To make a transition in DP, we should check whether it possible or not to fill the rest of the gap between the (*i* - 1)-th block and the *i*-th block. Lets say that (*i* - 1)-th block consists of number *x*, the *i*-th block consists of number *y*, and there are *f* still unfilled positions in the gap. Than the gap can be correctly filled if and only if 2·(*y* - *x* - 1) ≤ *f* ≤ 5·(*y* - *x* - 1).
     86 
     87 If you understand this DP, it won’t be difficult for you to find out how to construct the answer from it.