498.md (5405B)
1 Source: https://codeforces.com/blog/entry/15353 2 3 ## [499A - Watching a movie](/contest/499/problem/A "Codeforces Round 284 (Div. 2)") 4 5 One can solve the problem using greedy algorithm: if we can skip *x* minutes at current moment without skipping any good moment — we do that, otherwise — watch another minute of the film. 6 7 ## [499B - Lecture](/contest/499/problem/B "Codeforces Round 284 (Div. 2)") 8 9 In this task you must find for every string in the text the pair containing that string, and from two strings of that pair output the shortest one. 10 11 ## [498A - Crazy Town](/contest/498/problem/A "Codeforces Round 284 (Div. 1)") / [499C - Crazy Town](/contest/499/problem/C "Codeforces Round 284 (Div. 2)") 12 13 It can be easily proved that, if two points from statement are placed on different sides of some line, this line will be crossed anyway. So, all we need to do is to cross all these lines, so the answer is the number of these lines. 14 15 To check if two points lies on different sides of a line one can simply use its coordinates to place in line equation and check if these two values have different signs. 16 17 Solution complexity — *O*(*n*). 18 19 ## [498B - Name That Tune](/contest/498/problem/B "Codeforces Round 284 (Div. 1)") / [499D - Name That Tune](/contest/499/problem/D "Codeforces Round 284 (Div. 2)") 20 21 Let's numerate all the songs and seconds starting from 0. 22 23 Problem will be solved using DP approach. State will be described by two integers (*i*, *j*): *dp*[*i*][*j*] is probability of that we named exactly *i* songs, and the last named song was named exactly before *j*'th second (after *j* - 1 seconds). *dp*[0][0] = 1 obviously. 24 25 To make a move from state (*i*, *j*) to state (*i* + 1, *j* + *k*) (1 ≤ *k* < *t**i*), we must name the song exactly after *k* seconds its playing — probability of that is (1 - *p**i*)*k* - 1·*p**i*. 26 27 To fixed state (*i* + 1, *j*) sum of that moves can be represented as . Simple calculation of this value for each state gives *O*(*nT*2) complexity, so one must notice, that this values can be calculated using two pointers for fixed *i* (in common case it represent a segment with *t**i* length) for every *j* in time *O*(*T*). This way calculating this type of moves takes *O*(*nT*) time. 28 29 There is also a move to (*i* + 1, *j* + *t**i*) and a move from (*i*, *j*) to (*i*, (*j* + *k*) = *T*), when we couldn't name current song in time *T*. This types of moves is calculated with *O*(*nT*) too. 30 31 Solution complexity — *O*(*nT*). 32 33 ## [498C - Array and Operations](/contest/498/problem/C "Codeforces Round 284 (Div. 1)") / [499E - Array and Operations](/contest/499/problem/E "Codeforces Round 284 (Div. 2)") 34 35 We will divide only by prime numbers. 36 37 First, let's build a graph, where each of *n* numbers have own vertex group: 38 39 Find all prime factors of current number. Every factor will have its own vertex in a group, furthermore, if some factor *p* has power of *a**i* in current number, it will have exactly *a**i* vertexes in group. 40 41 The number of vertexes in such graph is . 42 43 Now we will make edges in our graph: edge between two vertexes exists if and only if there is a good pair (given in statement) of vertexes group numbers and the prime values of a vertexes are the same. That means that we can divide that group numbers by that prime. 44 45 The number of edges is . 46 47 Good pairs are given the way that our graph is bipartite. After finding maximum matching in this graph we represent the way of doing operations as described in the statement. 48 49 As soon as solution is using Kuhn's algorithm, its complexity is . One could notice that some of the edges are useless and reduce it to . 50 51 ## [498D - Traffic Jams in the Land](/contest/498/problem/D "Codeforces Round 284 (Div. 1)") 52 53 The solution of a problem — 60 (LCM of a numbers from 2 to 6) segment trees. 54 55 In *v*'th segment tree we will hold for every segment [*l*, *r*] the next value: minimum time needed to get from *l* to *r* if we start in a moment of time equal to *v* modulo 60. Using these trees' values it is easy to quickly answer the questions, carefully changing the trees' values. 56 57 ## [498E - Stairs and Lines](/contest/498/problem/E "Codeforces Round 284 (Div. 1)") 58 59 The problem is solved using DP approach *dp*[*i*][*mask*] — the number of ways to paint first *i* blocks of a ladder the way that the last layer of vertical edges is painted as described in mask *mask*. This could be easily recalculated using matrix *M*[*mask*1][*mask*2] — the number of ways to paint horizontal edges between two neighbour vertical layers painted as represented by masks *mask*1 and *mask*2. 60 61 For fixed *i* we have *w**i* layers, so this matrix must be multiplied by itself *w**i* times, which can be quickly done by binary-pow algorithm. After that this matrix is simply used in dynamic described above. 62 63 Solution complexity — .