### awoo's blog

By awoo, history, 2 years ago, translation, 1082A - Vasya and Book

Tutorial
Solution (Ajosteen)

1082B - Vova and Trophies

Tutorial
Solution (Ajosteen)

1082C - Multi-Subject Competition

Tutorial

1082D - Maximum Diameter Graph

Tutorial
Solution (PikMike)

1082E - Increasing Frequency

Tutorial

1082F - Speed Dial

Tutorial
Solution (PikMike)

1082G - Petya and Graph

Tutorial
Solution (Ajosteen)  Comments (51)
 » python 6-lines AC solution to problem B _, string = raw_input(), raw_input() start, end, res = 0, 0, 0 for char in string: if char == "G": start += 1 else: end, start = start, 0 res = max(res, start + end + 1) print min(res, string.count("G")) 
 » How to solve E
 » 2 years ago, # | ← Rev. 2 →   For problem E, I came up with an elegant (at least I thought during contest) O(nlogn) divide-and-conquer solution. I am just surprised to see the so easy linear solution after contest...
 » In problem D, why do we have to loop from the end of the array at the end of algorithm?I tried forward loop but gives WA on test 18... it seems that it doesn't construct tree well.
•  » » +1. having the same issue
•  » » So I just thought about it. The reason why the for loop needs to be done in reverse is because there is a chance that the very last node in the bamboo tree needs a "one" node to satisfy the diameter requirement that was previously calculated.Consider this test case 5 1 3 2 2 1 If you have both the bamboo construction for loop and the "ones add-on" for loop both go forwards, we'll add both one nodes to vertex 2 and never get the chance to append a one node to vertex 4 -- this would be required to make the diameter of the constructed graph 4, which would be the max.
•  » » » Now I got it, for the construction of the bamboo we go forward and but then we must go backward to use all the remaining edges.
 » Hello friends, Here is my solution to problem E. The pedagogy is quite different from the one in the editorial and it might be helpful. :)
 » 2 years ago, # | ← Rev. 2 →   My problem E solution is super simple.It might me helpful for you.https://codeforces.com/contest/1082/submission/46421135
•  » » Can you explain it?
•  » » » 2 years ago, # ^ | ← Rev. 2 →   In one word, It is like a parallel two pointer for every number.For every 'i', you need to change left of some numbers that count(left, i, X) < count(left, i, C), to 'i'. But you don't need to immediately calculate it for every number. Because count(i, i, vec[i]) is always 1.Sorry for my bad English.
•  » » » » No issues with your english but I couldn't get you
•  » » » » 2 years ago, # ^ | ← Rev. 2 →   Neighter do I. And it's really simple.
•  » » » » Especially this part. Can elaborate more? Because count(i, i, vec[i]) is always 1. 
•  » » » » » 2 years ago, # ^ | ← Rev. 5 →   If count(left, i, x) < count(left, i, c), we should change left to i. Then, its count is maybe 0. But We know answer always exist on some events increasing count about x. So, we don't have to change lefts immediatly. Therefore, If count(left, i, vec[i]) > count(left, i, c), we use it as it is. But else, we use it be changed about left. We can easily know we have to change left to i, and count(left, i, vec[i]) will be 1.Don't forget all x have different left.
•  » » » 11 months ago, # ^ | ← Rev. 2 →   I will explain how I understood his code. Let's define an array $num$, where $num$ $a_i$ is maximum length of a subsequence like this: $c, c, \dots, c, a_i, \dots, a_i, a_i.$ This is the subsequence of first i elements of array $a$. So, how to calculate this array $num$? $if$ $a_i=c$ we do nothing $else$ $num$ $a_i$ $=$ $max($ $pref$ $i$ $,$ $num$ $a_i$ $)+1$ This way we are either $1)$ starting a new sequence by appending $a_i$ to the sequence of $c$. or $2)$ extending $c, c, \dots, c, a_i, \dots, a_i, a_i.$ by appendind $a_i$ to the end. How does it help? Let $pref$ $i$ be number of $c$ in the prefix of array $a$. Say, we are at an index $i$. When we subtract $pref$ $i$ from $num$ $a_i$ , we get $cnt(l,i,a_i) - cnt(l,i,c)$ where $l$ is the index of first $a_i$ in the above sequence. Now, we iterate through all $i$ and find the maximum of $cnt(l,i,a_i) - cnt(l,i,c)$. Finally we add this value to $pref$ $n$ which is our answer. Note: $cnt$ is the same as defined in the editorial.
 » Could someone explain why we need this line in problem B, res = min(res, cntG); Thanks.
•  » » int nres = 1; we considered 'S' as 'G' to combine 'G' in the two sides, trying to get the longest subsegment. For examble:input 4 SGGGBefore res = min(res, cntG);, res = 4. It's an wrong answer.
•  » » » Got it. Thank you. :)
 » 2 years ago, # | ← Rev. 2 →   Update :- Got it :)
 » In the first case, Vasya can go directly to the y page from the x page if |x−y| is divided by d.In the second case, Vasya can get to page y through page 1, if y−1 is divided by d. The required number of actions will be equal to ⌈x−1d⌉+y−1d.Similarly, in the third case, Vasya can go to the page y through the page n if n−y is divided by d. The required number of actions will be equal to ⌈n−xd⌉+n−yd.why this? Please anyone explain.
 » 2 years ago, # | ← Rev. 2 →   In problem D, I was wrong in test 18. The vecdict is "Given diameter is incorrect 388 389", but in my output the diameter is 389? Anyone tell me why? Thanks so muck
•  » » The checker checks the diameter of your graph using your adjacency list, not just the diameter your output after "YES".
•  » » » Thanks u!
 » Here is my idea of E:We have a naive solution: consider each segment [l, r], get the maximum appearance of a number in the segment, assume it is x, then update result with cnt(1, l - 1, c) + x + cnt(r + 1, n, c), where cnt(l, r, j) is number of apperance of j in segment [l, r].In the solution above, for each segment [l, r], we choose the number with highest appearance and then change to c. But in fact, we can greedy change all the elements equal ar in segment [l, r]. Proof: If we choose the number with highest appearance, assume it is y, and y ≠ ar, then we can get better result if we choose segment [l, r'], where r' < r, cnt(r' + 1, r, y) = 0, y = ar', because cnt(r' + 1, n, c) ≥ cnt(r + 1, n, c) and cnt(l, r', y) = cnt(l, r, y), so cnt(1, l - 1, c) + cnt(l, r, y) + cnt(r + 1, n, c) ≤ cnt(1, l - 1, c) + cnt(l, r', y) + cnt(r + 1, n, c).So at the position i, let fai be the most number of c in segment [1, i] if you change all the elements equal ai to c in some segment [l, i]. Then: fai = max(fai + 1, cnt(1, i - 1, c) + 1). We update result with fai + cnt(i + 1, n, c).My submission: 46365450
•  » » I like this one. GJ
•  » » You saved my day
•  » » I understand that all a[i] in (l,r) can change to a[r] but : why calculate cnt( 1,i ,d ) only not cnt(1,l-1,c) and cnt(l,r,d)sorry for my poor English....
•  » » » At position i, fai is the maximum c we can get if we change all number equal ai in some segment [l,  i] to c. That means there is a segment [l,  i] which the sum cnt(1,  l  -  1,  c)  +  cnt(l,  i,  ai) is maximum and we suppose this sum equal fai. So we update result with cnt(1,  l  -  1,  c)  +  cnt(l,  i,  ai)  +  cnt(i  +  1,  n,  c)  =  fai  +  cnt(i  +  1,  n,  c).
•  » » What a wonderful solution!!!!
•  » » Why you can prove that it's optimal to change the elements equal ar in [l, r]? The proof seems to have only proved that changing ar' is better than changing ar. Could you please explain it in more detail?
•  » » » I mean that changing all numbers equal ar' = y to c in segment [l, r'] is better than changing all number equals y (which have the most apperance) in segment [l, r].
•  » » » » Got it! Thanks very much! : )
 » i used binary search for problem B
•  » » 2 years ago, # ^ | ← Rev. 2 →   Would you explain a bit how you used Binary Search?
•  » » » 46471253here it is.
 » Can anybody tell me why my code for problem C is giving WA?The link to my code : https://codeforces.com/contest/1082/submission/46672149
•  » » 2 years ago, # ^ | ← Rev. 2 →   Your solution gives wrong answer on test like: 2 2 1 1 2 -1 
•  » » » Thanks man.
•  » » » » What should be the output of this?
•  » » » » » It should be 1
 » 23 months ago, # | ← Rev. 2 →   B (1082B — Vova and Trophies) can also be solved with two pointers using constant additional memory. The idea is to find the longest substring with at most 1 silver (character S).Submission: 54993169