codeforces-scrape

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

37.md (3521B)


      1 Source: https://codeforces.com/blog/entry/798
      2 
      3 **A. Towers**  
      4  The total number of towers is equal to number of different numbers in this set. To get the maximum height of the tower, it was possible to calculate for each length the number of bars with this length, and from these numbers is to choose the maximum.  
      5    
      6  **B. Computer Game**  
      7  Constraints in the problem allows us to solve it this way: we keep the current number of health from the boss, and current summary damage from used scrolls per second. At the next step, we choose which scrolls we can use in the current second. Of all these, we find the scroll, which causes the most damage, and apply it. If at some point we could not use any of the scrolls, and the current damage in one second does not exceed regeneration, we deduce that there are no answers. Otherwise, continue to iterate the algorithm until the number hit points will be nonnegative.  
      8    
      9  **C. Old Berland Language**  
     10 One of the easiest to understand solutions of this problem is as follows: sort the words in ascending order of length, while remembering their positions in the source list. We will consistently build our set, starting with the short strings: strings of length one can only be strings "0" and "1". If the number of words of length one in a set are more than two, hence there are no answers. Add the desired number of strings of length one to answer, and remove it from the current list. Then look at the string of length two: each of the remaining strings of length one can be extended in two ways (having added to each of these symbols 0 and 1). Add the desired number of strings of length two in our answer, and then increase the length of the remaining strings by one. Continue this process, until we get all words from the input set. You can see that if at some moment the number of allowable words exceeded the number of remaining, the extra words can be ignored and solution takes O (N * the maximum length of input set) time.
     11 
     12 **D. Lesson Timetable**
     13 
     14 This problem is solved by dynamic programming:  
     15   
     16 state of dynamics will be a pair of numbers - the number of current room and number of groups which have first lesson in the room with a number not exceeding the current and for which the second room is not defined yet. For each state check all possible number of groups for which the second lesson will be held in the current classroom. When you add an answer from the new state, it must be multiplied by the corresponding binomial coefficients (the number of ways to select groups which have the first lesson in next room - *X**i* + 1, and the number of ways to select groups which have the second lesson in the current classroom).
     17 
     18 **E. Trial for Chief**
     19 
     20 First, we construct the following graph: each cell we associate a vertex of the same color as the cell itself. Between neighboring cells hold an edge of weight 0, if the cells share the same color and weight of 1, if different. Now, for each cell count the shortest distance from it to the most distant black cell (denoted by *D*). It is easy to see that we can construct a sequence of *D* + 1 repainting leads to the desired coloring:  
     21   
     22 
     23 * The first step of color all the cells at a distance less than or equal to *D* in black color
     24 
     25 * At the second step color all the cells at a distance less than or equal to *D* - 1 in white
     26 
     27 * Etc.
     28 
     29   
     30 Of all the cells, choose the one for which this distance is minimal, and this distance increased by one will be the answer to the problem.