658.md (8953B)
1 Source: https://codeforces.com/blog/entry/44047 2 3 Codes in nicer format will be uploaded later. 4 [658A - Bear and Reverse Radewoosh](/contest/658/problem/A "VK Cup 2016 - Round 1 (Div. 2 Edition)") and [639A - Bear and Displayed Friends](/contest/639/problem/A "VK Cup 2016 - Round 1") — [codes](http://ideone.com/NYuwV8). 5 [639B - Bear and Forgotten Tree 3](/contest/639/problem/B "VK Cup 2016 - Round 1") and [639C - Bear and Polynomials](/contest/639/problem/C "VK Cup 2016 - Round 1") — [codes](http://ideone.com/GcUNZJ). 6 [639D - Bear and Contribution](/contest/639/problem/D "VK Cup 2016 - Round 1") — [codes](http://ideone.com/FkE3DU). 7 [639E - Bear and Paradox](/contest/639/problem/E "VK Cup 2016 - Round 1") — [codes](http://ideone.com/OoSLF3). 8 [639F - Bear and Chemistry](/contest/639/problem/F "VK Cup 2016 - Round 1") — [codes](http://ideone.com/PUSLI2). 9 10 [658A - Bear and Reverse Radewoosh](/contest/658/problem/A "VK Cup 2016 - Round 1 (Div. 2 Edition)") — Iterate once from left to right to calculate one player's score and then iterate from right to left. It's generally good not to write something similar twice because you are more likely to make mistakes. Or maybe later you will find some bug and correct it only in one place. So, try to write calculating score in a function and run them twice. Maybe you will need to reverse the given arrays in some moment. 11 12 [639A - Bear and Displayed Friends](/contest/639/problem/A "VK Cup 2016 - Round 1") — You should remember all friends displayed currently (in set or list) and when you add someone new you must check whether there are at most *k* people displayed. If there are *k* + 1 then you can iterate over them (over *k* + 1 people in your set/list) and find the worst one. Then — remove him. The intended complexity is O(n + q*k). 13 14 [639B - Bear and Forgotten Tree 3](/contest/639/problem/B "VK Cup 2016 - Round 1") — You may want to write some special `if` for *n* = 2. Let's assume *n* ≥ 3. If *d* = 1 or *d* > 2*h* then there is no answer (it isn't hard to see and prove). Otherwise, let's construct a tree as follows. 15 16 We need a path of length *h* starting from vertex 1 and we can just build it. If *d* > *h* then we should also add an other path from vertex 1, this one with length *d* - *h*. Now we have the required height and diameter but we still maybe have too few vertices used. But what we built is one path of length *d* where *d* ≥ 2. You can choose any vertex on this path other than ends of the path (let's call him *v*), and add new vertices by connecting them all directly with *v*. You can draw it to see that you won't increase height or diameter this way. In my code I sometimes had *v* = 1 but sometimes (when *d* = *h*) I needed some other vertex and *v* = 2 was fine. 17 18 [639C - Bear and Polynomials](/contest/639/problem/C "VK Cup 2016 - Round 1"), my favorite problem in this contest. Let's count only ways to decrease one coefficient to get the required conditions (you can later multiply coefficient by - 1 and run your program again to also calculate ways to increase a coefficient). 19 20 One way of thinking is to treat the given polynomial as a number. You can find the binary representation — a sequence with 0's and 1's of length at most . Changing one coefficient affects up to  consecutive bits there and we want to get a sequence with only 0's. We may succeed only if all 1's are close to each other and otherwise we can print 0 to the output. Let's think what happens when 1's are close to each other. 21 22 Let's say that we got a sequence with two 1's as follows: ...00101000.... Decreasing by 5 one coefficient (the one that was once in a place of the current first bit 1) will create a sequence of 0's only. It's not complicated to show that decreasing coefficients on the right won't do a job (because the first 1 will remain there) but you should also count some ways to change coefficients on the left. We can decrease by 10 a coefficient on the left from first 1, or decrease by 20 a coefficient even more on the left, and so on. Each time you should check whether changing the original coefficient won't exceed the given maximum allowed value *k*. 23 24 One other solution is to go from left to right and keep some integer value — what number should be added to the current coefficient to get sum equal to 0 on the processed prefix. Then, we should do the same from right to left. In both cases maybe in some moment we should break because it's impossible to go any further. In one case it happens when we should (equally) divide an odd number by 2, and in the other case it happens when our number becomes too big (more than 2·109) because we won't be able to make it small again anyway. 25 26 [639D - Bear and Contribution](/contest/639/problem/D "VK Cup 2016 - Round 1") — It isn't enough to sort the input array and use two pointers because it's not correct to assume that the optimal set of people will be an interval. Instead, let's run some solution five times, once for each remainder after dividing by 5 (remainders 0, 1, 2, 3, 4). For each remainder *r* we assume that we should move *k* people to some value *x* that  (and at the end we want at least *k* people to have contribution *x*). Note that *x* must be close to some number from the input because otherwise we should decrease *x* by 5 and for sure we would get better solution. The solution is to iterate over possible values of *x* from lowest to highest (remember that we fixed remainder ). At the same time, we should keep people in 5 vectors/lists and do something similar to the two pointers technique. We should keep two pointers on each of 5 lists and always move the best among 5 options. The complexity should be *O*(*n*·5). 27 28 [639E - Bear and Paradox](/contest/639/problem/E "VK Cup 2016 - Round 1") — It's good to know what to do with problems about optimal order. Often you can use the following trick — take some order and look at two neighbouring elements. When is it good to swap? (When does swapping them increase the score?) You should write some simple formula (high school algebra) and get some inequality. In this problem it turns out that one should sort problems by a fraction  and it doesn't depend on a constant *c*. There may be many problems with the same value of  and we can order them however we want (and the question will be: if there is a paradox for at least one order). Let's call such a set of tied problems a block. 29 30 For each problem you can calculate its minimum and maximum possible number of granted points — one case is at the end of his block and the other case is to solve this problem as early as possible so at the beginning of his block. So, for fixed *c* for each problem we can find in linear time the best and worst possible scores (given points). 31 32 When do we get a paradox? Where we have two problems *i* and *j* that *p**i* < *p**j* (*p**i* was worth less points) we solved problem *i* much earlier and later we got less points for problem *p**j*. We can now use some segment tree or sort problems by *p**i* and check whether there is a pair of problems with inequalities we are looking for — *p**i* < *p**j* and *max**i* > *min**j* where *max**i* and *min**j* we found in the previous paragraph. 33 34 We can do the binary search over the answer to get complexity  or faster. Can you solve the problem in linear time? 35 36 [639F - Bear and Chemistry](/contest/639/problem/F "VK Cup 2016 - Round 1") — Task is about checking if after adding some edges to graph, some given subset of vertices will be in one biconnected component. Firstly, let's calculate biconnected components in the initial graph. For every vertex in each query we will replace it with index of its bicon-component (for vertices from subset and for edges endpoints). Now we have a forest. When we have a list of interesting vertices in a new graph (bicon-components of vertices from subset or edges) we can compress an entire forest, so it will containg at most 2 times more vertices than the list (from query) and will have simillar structure to forest. To do it, we sort vertices by left-right travelsal order and take LCA of every adjacent pair on the sorted list. If you have compressed forest, then you just have to add edges and calculate biconnected components normally, in linear time.