909.md (10943B)
1 Source: https://codeforces.com/blog/entry/56666 2 3 #### [909A - Generate Login](/contest/909/problem/A "Codeforces Round 455 (Div. 2)") 4 5 The most straightforward solution is to generate all possible logins (by trying all non-empty prefixes of first and last names and combining them) and find the alphabetically earliest of them. 6 7 To get a faster solution, several observations are required. First, in the alphabetically earliest login the prefix of the last name is always one letter long; whatever login is generated using two or more letter of the last name, can be shortened further by removing extra letter to get an alphabetically earlier login. 8 9 Second, the prefix of the first name should not contain any letter greater than or equal to the first letter of the last name, other than the first letter. 10 11 Thus, a better solution is: iterate over letter of the first name, starting with the second one. Once a letter which is greater than or equal to the first letter of the last name is found, stop, and return all letter until this one plus the first letter of the last name. If such a letter is not found, return the whole first name plus the first letter of the last name. 12 13 #### [909B - Segments](/contest/909/problem/B "Codeforces Round 455 (Div. 2)") 14 15 Consider a segment [*i*, *i* + 1] of length 1. Clearly, all segments that cover this segment must belong to different layers. To cover it, the left end of the segment must be at one of the points 0, 1, ..., *i* (*i* + 1 options), and the right end — at one of the points *i* + 1, *i* + 2, ..., *N* (*N* - *i* options). So the number of segments covering [*i*, *i* + 1] is equal to *M**i* = (*i* + 1)(*N* - *i*). The maximum of *M**i* over all *i* = 0, ..., *N* - 1 gives us a lower bound on the number of layers. 16 17 Because the problem doesn't require explicit construction, we can make a guess that this bound is exact. *max* *M**i* can be found in *O*(*N*); alternatively, it can be seen that the maximum is reached for  (for a central segment for odd *N* or for one of two central segments for even *N*). 18 19 The answer is . 20 21 We can also prove this by an explicit construction. Sort all segments in non-decreasing order of their left ends and then in increasing order of their right ends. Try to find a place for each next segment greedily: if *i* is the left end of current segment, and segment [*i*, *i* + 1] is free in some layer, add the current segment to that layer; otherwise, start a new layer with the current segment. 22 23 *and yes, this is our *O*(1) problem! :-)* 24 25 #### [909C - Python Indentation](/contest/909/problem/C "Codeforces Round 455 (Div. 2)") 26 27 This problem can be solved using dynamic programming. 28 29 Let's consider all possible programs which end with a certain statement at a certain indent. Dynamic programming state will be an array *dp*[*i*][*j*] which stores the number of such programs ending with statement *i* at indent *j*. 30 31 The starting state is a one-dimensional array for *i* = 0: there is exactly one program which consists of the first statement only, and its last statement has indent 0. 32 33 The recurrent formula can be figured out from the description of the statements. When we add command *i* + 1, its possible indents depend on the possible indents of command *i* and on the type of command *i*. If command *i* is a for loop, command *i* + 1 must have indent one greater than the indent of command *i*, so *dp*[*i* + 1][0] = 0 and *dp*[*i* + 1][*j*] = *dp*[*i*][*j* - 1] for *j* > 0. If command *i* is a simple statement, command *i* + 1 can belong to the body of any loop before it, and have any indent from 0 to the indent of command *i*. If we denote the indent of command *i* (simple statement) as *k*, the indent of command *i* + 1 as *j*, we need to sum over all cases where *k* ≥ *j*: . 34 35 The answer to the task is the total number of programs which end with the last command at any indent: . 36 37 The complexity of this solution is *O*(*N*2). 38 39 #### [909D - Colorful Points](/contest/909/problem/D "Codeforces Round 455 (Div. 2)") 40 41 We can simulate the process described in the problem step by step, but this is too slow — a straightforward simulation (iterate over all points when deciding which ones to delete) has an *O*(*N*2) complexity and takes too long. A solution with better complexity is required. 42 43 Let's consider continuous groups of points of same color. Any points inside a group are safe during the operation; only points at the border of a group are deleted (except for the leftmost point of the leftmost group and the rightmost point of the rightmost group, if these groups have more than one point). So, if current group sizes are, from left to right, *N*1, *N*2, ..., *N**G* - 1, *N**G*, group sizes after performing the first operation are *N*1 - 1, *N*2 - 2, ..., *N**G* - 1 - 2, *N**G* - 1, after the second operation — *N*1 - 2, *N*2 - 4, ..., *N**G* - 1 - 4, *N**G* - 2 and so on. This process continues until at least one of the groups disappears completely, at which point its adjacent groups may get merged if they are of the same color. 44 45 This way, multiple operations can be simulated at once: 46 47 1. Find the number of operations that are required for at least one group to disappear. 48 2. Update group sizes after this number of operations. 49 3. Remove empty groups. 50 4. Merge adjacent groups of the same color. 51 52 One update done this way requires *O*(*G*) time. During such an update at least one point from each group is deleted, so at least *O*(*G*) points are removed. If *N* is the initial number of points, we can remove at most *N* points in total. Therefore, running time of the algorithm is *O*(*N*). 53 54 #### [909E - Coprocessor](/contest/909/problem/E "Codeforces Round 455 (Div. 2)") 55 56 We want to minimize the number of communications between main processor and the coprocessor. Thus, we need to always act greedily: while there are tasks that can be executed on the main processor right away, execute them; then switch to the coprocessor and execute all tasks that can be executed there; then switch back to the main processor and so on. This can be done using breadth-first search. To run reasonably fast, this solution has to be implemented carefully: instead of searching for available tasks at each step, we want to maintain two queues of available tasks (for main processor and coprocessor) and add a task to a corresponding queue once all tasks it depends on has been executed. 57 58 Alternatively, we can define *T**i* as the lowest number of coprocessor calls required to execute *i*-th task, and derive a recurrent formula for *T**i*. If *u* is a task and *v*1, ..., *v**k* are its dependencies, then clearly for each *i* *T**u* ≥ *T**v**i* because *u* must be executed after *v**i*. Moreover, if *v**i* is executed on the main processor and *u* — on the coprocessor, then executing *u* will require an additional coprocessor call. Therefore, *T**u* = *max**i*(*T**v**i* + *s**i*), where *s**i* = 1 if *u* is executed on the coprocessor and *v**i* — on the main processor; otherwise, *s**i* = 0. Now all *T**i* can be calculated by recursive traversal of the dependency graph (or traversing the tasks in topological order). The answer to the problem is *max* *T**i*. 59 60 #### [909F - AND-permutations](/contest/909/problem/F "Codeforces Round 455 (Div. 2)") 61 62 ##### Permutation *p* (*p**i* & *i* = 0) 63 64 If *N* is odd, the answer is NO. Indeed, any number in odd-numbered position *i* *p**i* must be even, otherwise the last bit of *p**i*&*i* is 1. For odd *N* there are less even numbers than odd-numbered positions, so at least one of the positions will hold an odd number, thus it's impossible to construct a required permutation. 65 66 If *N* is even, the required permutation exists. To build it, first observe that (2*k* - *i*)&(2*k* + *i* - 1) = 0. For example, for *k* = 5: 67 68 100000 = 25 69 70 011111 = 25 - 1 71 72 100001 = 25 + 1 73 74 011110 = 25 - 2 75 76 and so on. 77 78 We can use this fact to always match 2*k* - *i* and 2*k* + *i* - 1 with each other, that is, set *p*2*k* - *i* = 2*k* + *i* - 1 and *p*2*k* + *i* - 1 = 2*k* - *i*. 79 80 The full procedure for constructing the required permutation is as follows. For a given even *N*, find the maximum power of two that is less than or equal to *N* 2*k*. Match pairs of numbers 2*k* - *i* and 2*k* + *i* - 1 for each *i* = 1..*N* - 2*k* + 1. If we are not done yet, numbers from 1 to 2*k* - (*N* - 2*k* + 1) - 1 = 2*k* + 1 - *N* - 2 are still unmatched. Repeat the process for *N*' = 2*k* + 1 - *N* - 2. 81 82 For example, for *N* = 18 on the first step we set 2*k* = 16 and match numbers 15-16, 14-17 and 13-18. On the second step unmatched numbers are from 1 to 12, so we set 2*k* = 8 and match numbers 7-8, 6-9, 5-10, 4-11 and 3-12. On the third and the last step the remaining unmatched numbers are 1 and 2, so we set 2*k* = 2 and match numbers 1 and 2 with each other. After this no unmatched numbers are left, and we are done. 83 84 ##### Permutation *q* (*q**i* & *i* ≠ 0) 85 86 We can do a simple case analysis for *N* = 1..7 manually, noticing that the answer is NO for *N* = 1..5, a possible answer for *N* = 6 is \textbf{3 6 2 5 1 4} as given in problem statement, and a possible answer for *N* = 7 is \textbf{7 3 6 5 1 2 4}. 87 88 If *N* is a power of two, then it is represented in binary as 10..0. We must have *q**N* ≠ *N*, therefore *q**N* < *N*, so the binary representation of *q**N* is shorter than that of *N*. It follows that *q**N*&*N* = 0, so the answer is NO in this case. 89 90 Finally, if *N* > 7 and *N* is not a power of two, the required permutation always exists, and can be built in the following way. Split all numbers from 1 to *N* into the following groups (*k* is the largest power of two which is still less than *N*): 91 92 1..7 93 94 8..15 95 96 16..31 97 98 \ldots 99 100 2*k* - 1..2*k* - 1 101 102 2*k*..*N* 103 104 For the first group use the permutation that we found manually. For each of the remaining groups, use any permutation of numbers in this group (for example, a cyclic permutation). The numbers in each group have leading non-zero bit at the same position (which corresponds to the power of two at the beginning of the group), so it is guaranteed that *q**i*&*i* contains a non-zero bit at least in that position.