366.md (4586B)
1 Source: https://codeforces.com/blog/entry/9704 2 3 [366A — Dima and Guards](https://codeforces.com/contest/366/problem/A) 4 5 The solution doesn't exist only if the mimimal way to bribe is grater than *n*. Otherwise we can increase the gift price to make *price*1 + *price*2 = *n*. 6 7 Let's find the miminal way to bribe. We should buy that gift for old lady which costs less. It means, if we have 2 guards with params *a* *b* *c* *d*, then minimum bribe price will be *min*(*a*, *b*) + *min*(*c*, *d*). Let's choose the guard with the minimal bribe price. If the minimal bribe price is greater than *n*, answer -1. Otherwise, the possible answer is, for example: Guard number, *min*(*a*, *b*), *n*–*min*(*a*, *b*). 8 9 [366B — Dima and To-do List](https://codeforces.com/contest/366/problem/B) 10 11 Dima can make *k*–1 tasks, so Inna always tells him off for each *k*-th taks, beginning from the chosen place. So for the numbers with same modulo by k the answer will be the same. We should find the answer with minimal first task number so it is eniugh to calculate the sums of “tellings of” for tasks 0, 1... *k*–1. We can do it in such way: Determine the array int *sum*[*k*]. And put the number into appropriate bucket while reading: 12 13 *sum*[I mod k] + = *a*[*i*]. Now we should simply find first *i* with minimal *sum*[*i*]. 14 15 Complexity: *O*(*N*). 16 17 [366C — Dima and Salad](https://codeforces.com/contest/366/problem/C) 18 19 Let's calculate dinamic: *d*[*num*][*balance*] where *num* – last looked fruit, balance — difference between sum of colories and sum of tastes. Let's multiply each *b* by *k*. The answer will be *d*[*n*][0]. 20 21 *d*[*num*][*balance*] = maximal possible sum of tastes under conditions. 22 23 Step: from *d*[*num*][*balance*] we can relax answers: 24 25 *d*[*num* + 1][*balance*] = *max*(*d*[*num* + 1][*balance*], *d*[*num*][*balance*]) – if we don't choose a fruit 26 27 *d*[*num* + 1][*balance* + *a*[*i*] - *b*[*i*]·*k*] = *max*(*d*[*num* + 1][*balance* + *a*[*i*] - *b*[*i*]·*k*], *d*[*num*][*balance*] + *a*[*i*]) – if we choose a fruit. 28 29 Balance can be negative, so in programming languages which don't support negative indexation, indexes should be shifted by the biggest negative number for balance. If we determine the balance as *sum*(*b*·*k*) - *sum*(*a*) it will be the sum of all tastes. 30 31 [366D — Dima and Trap Graph](https://codeforces.com/contest/366/problem/D) 32 33 The answer for some path is a range under which we can go all the way, and this range is the intersection of all the ranges on the path. We can conclude it because the number is valid for path if it is valid for all ranges in the path. We will iterate all ribs. Let the left board of range on a rib is the left board of our intersection. It means we can't use ribs whith left board greater than ours. Lets iterate all right boards, determining the answer as the range from fixed left board to the chosen right. This answer exists if we have any path from the first node to the last. Let's check if the graph is connected if we leave only ribs with left board not greater than our and right board not less than our. If the graph is connected — let's update the answer. Right board can be found by binary search so the complexity is *O*(*M*2·*logM*). 34 35 [366E — Dima and Magic Guitar](https://codeforces.com/contest/366/problem/E) 36 37 There are many solutions for this task. I will describe my, you can deal with other by looking participants code. To find the answer we should calculate *maxDis*[*k*][*k*], where *maxDis*[*i*][*j*] – maximal complexity from note *i* to note *j*. 38 39 Now we should only iterate the song updating answer for each pair of adjacent notes. Let's think how we can calculate the matrix. 40 41 For each place (*x*1, *y*1) on the guitar let's iterate pairs (*x*2, *y*2) with *y*2 ≤ *y*1. 42 43 If (*x*2 ≤ *x*1) distance will be *x*1–*x*2 + *y*1–*y*2. So we should find minimal *x*2 + *y*2 in submatrix from (0, 0) to (*x*, *y*). 44 45 If (*x*2 ≥ *x*1) distance will be *x*2–*x*1 + *y*1–*y*2. So we should find maximal *x*2–*y*2 in submatrix from (*n*–1, 0) до (*x*, *y*). 46 47 We will calculate this values for each note. We need too much memory so we should memorize only one previous row for each note. For each place we will update dinamics for both variants according to already calculated and for our own note (which is in this cell) we will also compare (*i* + *j*) or (*i*–*j*) with current value in the cell. 48 49 Complexity *O*(*N*·*M*·*K*)