vovuh's blog

By vovuh, history, 7 years ago, In English

961A - Tetris

Editorial
Solution (Vovuh)

961B - Lecture Sleep

Editorial
Solution (Vovuh)

961C - Chessboard

Editorial
Solution (Ajosteen)

961D - Pair Of Lines

Editorial
Solution (adedalic)

961E - Tufurama

Editorial
Solution (Ajosteen)

961F - k-substrings

Editorial
Alternative solution (jtnydv25)
Solution (adedalic, suffix array)
Solution (adedalic, hashes)

961G - Partitions

Editorial
Solution (adedalic)
  • Vote: I like it
  • +38
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

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.net/contest/961/submission/36984273

»
7 years ago, # |
  Vote: I like it +1 Vote: I do not like it

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!

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
7 years ago, # |
  Vote: I like it +6 Vote: I do not like it

It's beautiful problemset, thx!

»
7 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Geomety always seems to get me..

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone give me more detailed explanation of question B?

  • »
    »
    7 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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.net/contest/961/submission/36992860

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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<i+1 (j<=i) because they can't pair with further seasons. Now I need to find those indices which are in priority queue and are less than a(i+1) because if index is greater than a(i+1) it won't be able to pair with season(i+1). For doing this I use an AVL. Whenever I am popping from priority queue I delete the corresponding index from AVL. I then count all indices lesser than (ai+1) and it to my final answer. Then finally I insert the pair (a[i+1],i+1) to priority queue and index i+1 to AVL. (Both operations take LogN complexity). It was much easier to code and code for AVL is easily available on GFG. My Solution

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have made the same, but with Fenwick tree (BIT), much more simple to code.

»
7 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Nice F

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Yah ! I also sloved it using merge sort tree.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone help me in understanding Problem E?

  • »
    »
    7 years ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it

    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.

»
7 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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 36971807

Most of the shortest answers are done by this way.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Could you elaborate on your method? Looks interesting

    • »
      »
      »
      7 years ago, # ^ |
      Rev. 2   Vote: I like it +3 Vote: I do not like it

      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].

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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?

        • »
          »
          »
          »
          »
          7 years ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          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).

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    nice approach :)

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For problem D what would be the approach if maximum number of lines allowed were 3?

  • »
    »
    7 years ago, # ^ |
    Rev. 3   Vote: I like it +3 Vote: I do not like it

    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.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone help me in understanding Problem c?i am not getting tutorial too.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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

»
7 years ago, # |
  Vote: I like it +3 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it +8 Vote: I do not like it

How to solve problem D for N points and K lines?

»
7 years ago, # |
  Vote: I like it +34 Vote: I do not like it

Something about Problem G. I guess most of us get the formula below easily:

But the Editorial above claims:

So may anybody prove that:

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I would gladly hear the formal proof of equation myself since it doesn't sound like easy transformation.

  • »
    »
    7 years ago, # ^ |
    Rev. 6   Vote: I like it +1 Vote: I do not like it

    Here is a proof I thought of. Also, sorry for too many revisions :P

    Consider 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 .

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +14 Vote: I do not like it

      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.

      • »
        »
        »
        »
        7 years ago, # ^ |
        Rev. 2   Vote: I like it +5 Vote: I do not like it

        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.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can someone please explain me F. I didn't get the editorial :(

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Here is my blog post on the very elegant geometry problem D :)

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

i feel like my brain expanded after solving E xD

»
4 years ago, # |
Rev. 4   Vote: I like it +8 Vote: I do not like it

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 abbaabbaabba

If 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

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I somehow happened to solve a problem in this round, to notice your comment :)

    The case looks to be the famous one: https://codeforces.net/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).

»
2 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
22 months ago, # |
  Vote: I like it 0 Vote: I do not like it

The link for the alternative solution of problem F is broken. Here's the link: https://codeforces.net/blog/entry/58707?#comment-423461