### vovuh's blog

By vovuh, history, 5 years ago,

961A - Tetris

Editorial
Solution (Vovuh)

961B - Lecture Sleep

Editorial
Solution (Vovuh)

961C - Chessboard

Editorial
Solution (Ajosteen)

961D - Pair Of Lines

Editorial

961E - Tufurama

Editorial
Solution (Ajosteen)

961F - k-substrings

Editorial
Alternative solution (jtnydv25)

961G - Partitions

Editorial

• +38

| Write comment?
 » 5 years ago, # | ← Rev. 2 →   +9 Damn, I used Order Statistics tree to solve Problem E after the contest, and now I find it can be done with BIT.http://codeforces.com/contest/961/submission/36984273
•  » » 5 years ago, # ^ |   0 Can you explain your approach ?
 » 5 years ago, # |   +1 nice problemset :)Had solved E in 3 hours by using persistence segment tree. By seeing the editorial i think that i know nothing :)Nice editorial!
•  » » 5 years ago, # ^ |   0 I solved E with persistence segment tree too, but it didn't took too much time because the only difference between normal segment tree and persistence segment tree is that you have to change the update process of later.
 » 5 years ago, # |   +6 It's beautiful problemset, thx!
 » 5 years ago, # |   +6 Geomety always seems to get me..
 » 5 years ago, # |   0 Can someone give me more detailed explanation of question B?
•  » » 5 years ago, # ^ | ← Rev. 2 →   0 First, in O(N) time, we can calculate "total", the current total number of theorems that Mishka will be able to write without the secret technique.Now we calculate the EXTRA number of theorems (apart from the ones for which Mishka needs no help) that Mishka will be able to write if the secret technique will get used at the first minute; this is O(K) of course. Call this number "cumu". Now in O(1) time, we can find out the number of theorems that Mishka can write down if the secret technique will get used at the second minute (just remove the number of theorems for the first minute (if applicable) and add the number of theorems for the (K+1)th minute (if applicable)). Keep "sliding" till you reach the end. Of course, maintain a maximum of what you have seen so far, which we call maxcumu.Finally, add "maxcumu" to "total". http://codeforces.com/contest/961/submission/36992860
•  » » » 2 months ago, # ^ | ← Rev. 2 →   0 shouryap , I tried a very similar approach. Instead of counting the theorems where Mishka was asleep, I added all the theorems in that segment and removed the ones which Mishka could or couldn't write. It certainly fails for a testcase, couldn't understand why my approach is not working. Though, it is simulating exactly what you said (am I right?). Can you please help?Submission: 174204661
 » 5 years ago, # |   0 I used just the priority queue and AVL for E and it just worked fine. In min priority queue I save pair of (ai,i). On reaching i+1 I pop all those ai from priority queue where aj
•  » » 5 years ago, # ^ |   0 I have made the same, but with Fenwick tree (BIT), much more simple to code.
 » 5 years ago, # |   +4 Nice F
 » 5 years ago, # |   0 Amazing problemset. I solved E after the contest using merge sort tree, it was pretty intuitive, and good oportunity to learn to code merge sort tree (because i didnt implement it before, only knew idea). Anyone else solved it this way?
•  » » 5 years ago, # ^ |   +1 Yah ! I also sloved it using merge sort tree.
 » 5 years ago, # |   0 Can anyone help me in understanding Problem E?
•  » » 5 years ago, # ^ | ← Rev. 2 →   +3 Observe that for some season i we'll only get a collision with an episode of season j where j < i if aj >= i and ai >= j. So we just need to able to answer for a particular season i how many seasons with index j such that j < i have aj >= i and ai >= j. We can do this query for all i (using a merge-sort tree for example) and then report the answer.
 » 5 years ago, # | ← Rev. 2 →   0 For E, just use BIT to maintain sorted segments. Then use binary search (lower_bound in C++) to get the answer in each segment. I think it's the most simplest approach (or the best answer especially in a time highly limited competition).Here is my code 36971807Most of the shortest answers are done by this way.
•  » » 5 years ago, # ^ |   0 Could you elaborate on your method? Looks interesting
•  » » » 5 years ago, # ^ | ← Rev. 2 →   +3 Well. In this problem, we need to calculate how many numbers are greater than i in the range [i + 1, a[i]].Let d[l, r, x] denote how many numbers are greater than x in the range [l, r].Obviously, d[l, r, x] = d[1, r, x]-d[1, l-1, x] (prefix sums)Then you can use Binary Indexed Tree (BIT) with vector, which means each node of the BIT is a vector that collects numbers in a range, just like the node of a typical BIT sums numbers in the range.After sorting each vector, you can use binary search to get answers in each vector. By using BIT query method, you can form prefix sums and get d[l, r, x].
•  » » » » 5 years ago, # ^ |   0 What will be the complexity of this solution? Like if You sort vectors in every node of BIT? Won't it be too much? Further, what will be the max size of a any vector representing a node?
•  » » » » » 5 years ago, # ^ | ← Rev. 2 →   0 Each number appears in O(log n) vectors. So the space complexity is O(n log n). Time complexity is O(n log^2 n).Well, the max size of a vector is O(n). However, the average size of vectors is just O(log n).
•  » » 2 years ago, # ^ |   0 nice approach :)
 » 5 years ago, # |   0 For problem D what would be the approach if maximum number of lines allowed were 3?
•  » » 5 years ago, # ^ | ← Rev. 3 →   +3 This is one thing I could think of; I am sure we can do better. Assume n >= 4, and assume that the n points lie on 3 lines. Consider any four points (say, the first ten points given). By Pigeonhole Principle, some 2 points lie on the same line. And well, take all cases. Once you are done finding one of the lines, the problem reduces to the given problem.Another less "casework" way considers n>= 10. Consider the first 10 points. Again by Pigeonhole Principle, some 4 points lie on the same line, and (I guess) we can also prove that if four points are collinear, then one of the lines must pass through them. So just find a quadruple of points that are collinear. If there does not exist a quadruple, then our assumption is false, otherwise the quadruple determines one line, and the problem has been reduced to D.
•  » » 5 years ago, # ^ |   0 Another solution which can be easily generalized for more than two lines is: pick two different points at random, remove them and all points collinear with them and repeat until you have drawn expected number lines, and repeat the whole process until you have found way to partition all of the points or until clock() < TIME_LIMIT * CLOCKS_PER_SEC. Suppose pn points lie on first line and qn on the second (p + q) = 1 then probability of choosing points lying on the same line is roughly p2 + q2 which is greater or equal to ). In case of 3 lines we have at least chance for our first guess and at least for our second guess. which is total (or for k lines ). For k ≤ 5 this should be enough.
•  » » » 5 years ago, # ^ | ← Rev. 2 →   +10 But the random can be hacked in open hacking……like 36960802
•  » » » » 5 years ago, # ^ |   0 It can't be if I seed random number generator with the hash of the input. Or at least hacking it would be much more difficult.
 » 5 years ago, # |   0 Can anyone help me in understanding Problem c?i am not getting tutorial too.
•  » » 5 years ago, # ^ |   0 We have 4 pieces of board so to make a new chessboard out of the 4 we need 2 pieces with black cell on the (0,0) position and 2 pieces with white cell on the (0,0) position. Also note that once we know the color of (0,0) position we can make the whole chessboard out of it. So we find the cost of converting each piece into two chessboards — one with white cell at (0,0) and another with black cell at (0,0) and then we calculate the minimum of all possible ways. My solution
 » 5 years ago, # |   +3 Just wanted to mention that F is essentially the same (actually more explicit) as an old POI problem: https://main.edu.pl/en/archive/oi/19/pre
 » 5 years ago, # |   +8 How to solve problem D for N points and K lines?
 » 5 years ago, # |   +34 Something about Problem G. I guess most of us get the formula below easily: But the Editorial above claims: So may anybody prove that:
•  » » 5 years ago, # ^ |   0 I would gladly hear the formal proof of equation myself since it doesn't sound like easy transformation.
•  » » 5 years ago, # ^ | ← Rev. 6 →   +1 Here is a proof I thought of. Also, sorry for too many revisions :PConsider n students who wish to participate in a contest. They must enter themselves in exactly k non-empty groups. One of these groups is a special group who gets to participate in Div 1, while the others participate in Div 2. Furthermore, Alice, one of the students, is very good at coding, so she will always participate in Div 1. Furthermore, the special group has a team leader. We count the number of ways C of partitioning the students in two ways.First, we select, out of n - 1 students, j - 1 students who, along with Alice, form the special group. This can be done in ways. Now the n - j normal students can be partitioned in ways, and the team leader of the special group can be chosen in j ways. Thus .Another way is this: Suppose Alice is the team leader. Then simply partition the n students into k groups in ways, and the team Alice belongs to is special. Otherwise choose a team leader out of the remaining students in n - 1 ways (call this chosen leader Bob), and partition the n - 1 students other than Bob in ways. The one Alice belongs to is the special group, so Bob belongs to that group of course. Therefore .
•  » » » 5 years ago, # ^ |   +14 Yes, you are right, but you make exactly what problem G asks. Your proof is just a more formal than from editorial. In other words, formulas are same because they count same thing.What I (and probably, Blue233333) expected are just formal equivalent transformations of left and right parts using some properties of Binomial Coefficients and Stirling numbers.
•  » » » » 5 years ago, # ^ | ← Rev. 2 →   +5 With swm_sxt and zsnuo's help, I find an approach to use equivalent transformations to prove that, but I am still a little confused. However, I don't understand the last step's formula transformations very clearly, while I could only understand it with this: Dividing n people into k non-empty groups, means that we first choose j, 0 ≤ j ≤ n - 1, people to be members of person n's group, and then divide the remained persons into k - 1 groups.
•  » » » » » 4 years ago, # ^ |   0 You can check it at wikipedia.
 » 5 years ago, # |   0 There are n circles drawn on a piece of paper in such a way that any two circles intersect in two points, and no three circles pass through the same point. Turbo the snail slides along the circles in the following fashion. Initially he moves on one of the circles in clockwise direction. Turbo always keeps sliding along the current circle until he reaches an intersection with another circle. Then he continues his journey on this new circle and also changes the direction of moving, i.e. from clockwise to anticlockwise or vice versa. Suppose that Turbo’s path entirely covers all circles. Prove that n must be odd.
•  » » 5 years ago, # ^ |   0 This is from IMO. Also, it is unrelated.
 » 5 years ago, # |   0 can someone please explain me F. I didn't get the editorial :(
 » 4 years ago, # |   0 Here is my blog post on the very elegant geometry problem D :)
 » 4 years ago, # |   0 i feel like my brain expanded after solving E xD
 » 22 months ago, # | ← Rev. 4 →   +8 I would like to share my solution for problem F.Given a string s let's create a new string t of double size concatenating the first character with the last character, the second with the penultimate, and so on.. For example, given ababab we got abbaabbaabbaIf you look carefully at this new string, we are now interested in palindromes! (convince yourself about this). Of course, we are not interested in all palindromes but some of them. More precisely we are interested in the palindromes that meet these requirements (Let L be the beginning of the palindrome, R the end, and n the length of the original string): L is an even position R is an odd position L is in the first half of the string (this is because is symmetric) (R-L+1)/2 is odd (this is a requirement of the problem) n-R/2-1 > L/2 (is not a proper prefix-suffix) So we can iterate over all the biggest palindromes and then just calculate the answer for each substring and propagate the answer to small ones.The solution is O(N);Here is the link to my submission
 » 18 months ago, # |   0 I realise this is an old contest but I'm just trying it now. Is test case 34 on F some sort of anti-hashing hack? I tried a polynomial rolling hash with three different (prime) values for x, all working mod 2^64, and they all fail on this case (here, here and here). When I changed the modulus to 10^9+7, it passed, even though this gives a far smaller range of possible hash values.
•  » » 17 months ago, # ^ |   0 I somehow happened to solve a problem in this round, to notice your comment :)The case looks to be the famous one: https://codeforces.com/blog/entry/4898. In short, the polynomial is something like $(1 - x) (1 - x^2) (1 - x^4) (1 - x^8) \cdots$ and any odd $x$ makes it divisible by $2^{64}$. For a prime modulus, we can have a probability bound from the fact that a non-zero polynomial of degree $n$ has at most $n$ roots (Schwartz–Zippel lemma).
•  » » » 17 months ago, # ^ |   0 Thanks, I learned something new.
 » 6 months ago, # |   0 Problem B is just an extended version of finding maximum subarray sum of positive integers in O(n) space. Good problem.
 » 3 months ago, # | ← Rev. 3 →   0 I had a problem with D, test case 49 seems YES to me but the wanted answer is NO. All the points lie in the same line.All the lines passing throw $p[i]$ and $p[0]$ (where $1 \le i \le 5$) are with the exact same slope $1.61803398875$. Here are the points plotted with Desmos My submission: 172444322
•  » » 3 months ago, # ^ |   0 The solution passed when I got rid of all double in the code. What I learned here is to think about doubles as monsters that will eat so I have to run away.