Vovuh's blog

By Vovuh, history, 4 weeks ago, In English,

1196A - Three Piles of Candies

Idea: MikeMirzayanov

Tutorial
Solution

1196B - Odd Sum Segments

Idea: Vovuh

Tutorial
Solution

1196C - Robot Breakout

Idea: MikeMirzayanov and Vovuh

Tutorial
Solution

1196D1 - RGB Substring (easy version)

Idea: MikeMirzayanov

Tutorial
Solution

1196D2 - RGB Substring (hard version)

Idea: MikeMirzayanov

Tutorial
Solution

1196E - Connected Component on a Chessboard

Idea: MikeMirzayanov

Tutorial
Solution

1196F - K-th Path

Tutorial
Solution
 
 
 
 
  • Vote: I like it
  • +42
  • Vote: I do not like it

»
4 weeks ago, # |
  Vote: I like it +43 Vote: I do not like it

Since we have the proof that the structure described in the problem E editorial is optimal, but Vovuh didn't post it for some reason, I'll try to explain it.

Consider the case $$$w \le b$$$. The structure from the editorial can handle at least $$$w - 1$$$ and at most $$$3w + 1$$$ black cells. It is impossible that $$$b < w - 1$$$, since $$$w \le b$$$. So we have to prove that there's no structure of component capable of supporting more than $$$3w + 1$$$ cells.

We can surround each white cell with four black cells, so we can't support more than $$$4w$$$ cells — but even this is possible only if $$$w = 1$$$, because we need do somehow connect all white cells. For a fixed black cell $$$i$$$, let's denote the number of white cells adjacent to it as $$$k_i$$$. The sum of $$$k_i$$$ over all black cells cannot exceed $$$4w$$$. Some cells can have $$$k_i = 1$$$, but if we want to connect some white cells, we have to use a black cell that has $$$k_i > 1$$$.

Let's analyze the sum of $$$(k_i - 1)$$$ over all black cells. I state that it is not less than $$$w - 1$$$, because we need at least $$$w - 1$$$ ''connections'' to connect all white cells. So, each value of $$$k_i$$$ is at least $$$1$$$, $$$\sum k_i \le 4w$$$, and $$$\sum (k_i - 1) \ge w - 1$$$. It means that there can be at most $$$3w + 1$$$ summands in these sums, so we can have at most $$$3w + 1$$$ black cells.

»
4 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

There's a typo, problem A links to 1015A instead of 1196A.

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

Wouldn't Floyd Warshall be too much for around 800 vertices?

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    Because of some vectorization and other compiler optimizations it works very fine (around 1-1.2s in the worst case). But yes, I know that for interpreted language this feature doesn't work and this is sad :(

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +20 Vote: I do not like it

    I mean, if you run it for each component independently then worsecase is improved by a factor of 8... there's no excuse for being lazy :)

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

      Yeah well I didn't even come up with the Floyd Warshall idea during contest so t.t

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Just use dijkstra on all vertices. That gives O(n*m*log(n)) solution.

    • »
      »
      »
      4 weeks ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Will it be within Time limit? (Considering n and m are in the range of 10^5).

      • »
        »
        »
        »
        4 weeks ago, # ^ |
          Vote: I like it +8 Vote: I do not like it

        I meant the reduced n and m.

        You would have to reduce the graph as described in the editorial to atmost 2k vertices and k edges. In terms of the original constraints that will be: O(k*k*log(k)).

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Great editorial! All the solutions are very nicely explained.

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

Can anyone explain B to me please? And if we print k-1 positions of odd numbers, what if there are more than one numbers of odd numbers in a segment? Please help. Thanks.

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

    you can see the problem as we printing the k-1 positions of odd numbers, so making k-1 segments with just one odd number, and for the last segment, we just need to see if with the remaining odd we can create a segment that the sum of its elements is odd, that is, if (cnt — k)%2 == 1.

    as we are printing the k-1 positions in order, each one of the segment have just one number, because the previous one odd number is the previous segment, because we printed it position.

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

      (cnt-k+1)%2 == 1**, and with some manipulation you can get the cnt%2 == k%2

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

        Can you please show the manipulation to get cnt%2==k%2

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

      Thank you very much, I understood the idea.

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

      Hey, I got something in my mind I know it is ridiculous but this does not make a difference right?(I know it doesn't but when I think I fell like I can't proove that,is it normal? :D): If we have more than one numbers of odd numbers in any segment in k-1 segments.

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

        Or is it like : If we want to change a segments number of odd numbers (lets say it is 1 and the last segment is bigger) we need to add 2 numbers to make numbers odd again and that means we remove 2 odd numbers so last segment's number of odd numbers's mod 2 didn't change am I rigth? I know my ideas are ridiculous.

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

          The mod 2 didn't change, so If you wanted add more number in one segmento, you can subtract for other segment that have 3 or more odd number. It's not like the k-1 need to just have one odd, we do it because after that we just need to care about just one segmento( the least one)

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

            Thank you very much. You helped me a lot.

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

Does someone use DP to pass the problem D.

You can make dp[i][j] mean from the i-th character and start with j=0('R'),j=1('G'),j=2('B')

make tab={'R','G','B'},ADD=(k-1)%3

so:

dp[i][0]=dp[i-1][2]-(s[i-2]!='B')+(s[i+k-2]!=tab[ADD]);

dp[i][1]=dp[i-1][0]-(s[i-2]!='R')+(s[i+k-2]!=tab[(ADD+1)%3]);

dp[i][2]=dp[i-1][1]-(s[i-2]!='G')+(s[i+k-2]!=tab[(ADD+2)%3]);

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

The best Div 3 ever ! Fast and quality

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Why do we need only min(k,m) edges for problem F?

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    If k>m, then it's obvious that we have to take all m edges given. But if k<=m, we can choose the first k edges after sorting the edges according to weighs. Our ans in this case can not exceed the value of weight of kth edge. And if the required ans is less than it, then all the required edges for the kth shortest path is already included in these first kth edges.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

why there are two Tutorials

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

The solution to E is very beautiful

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

I don't understand why doesn't need more than min(k,m), somebody knows the reason for this it

»
4 weeks ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

How to solve F for greater K? I read about Yen's algorithm for single source K-th shortest path. But this one is different.

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

    If you use Dijkstra, you'll solve it in O(k*k*log(k)), so it supports K in the order of thousands (6.000 ~ 10.000 aprox)

    • »
      »
      »
      4 weeks ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Could you please explain this solution more? And why does it have this complexity?

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

        You make a new graph with the minimum K edges, you'll find the answer among this edges for sure, and if the answer is not an edge by itself, it will be a path that contains some of these edges. Maybe this is the tricky part. The Dijkstra part is easy, you need to run one Dijkstra per the new graph's nodes. So given Dijkstra's complexity is E*log(V) , the edges is K and the nodes is also K, so for each run the complexity is O(K*log(K)) and you run one Dijkstra for each node, so K*K*log(K)

        • »
          »
          »
          »
          »
          4 weeks ago, # ^ |
            Vote: I like it +3 Vote: I do not like it

          Oh, OK. So it's the same as the editorial solution but you just use a Dijkstra from every node instead of Floyd-Warshall. Thanks!

»
4 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

I was lazy to read the A statement, decided to read examples and send code.

It was easier to solve the problem with examples instead of the statement :D

Want moar!

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

I wrote n dijkstra's algorithms in F. I suppose it works in $$$O(nklog(n))$$$. It still passed in 450ms.

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

Please tell me where I am making mistake in solution of D1: 1) I make three strings of length K each starting with R,G,B. eg k=3 s[0]="RGB",s[1]="GBR",s[2]="BRG" 2) I am checking the longest common substring with each of the three strings with the given string and storing in mx. 3)return answer as k-mx; Pls Help /. My Solution

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

    Checking the longest common substring does not accurately count how many characters you need to change to get the desired RGB sequence. For example, calling lcs("RGBRGB", "RRRRRR") returns $$$1$$$ (meaning that your answer will be $$$5$$$), while you only need to change $$$4$$$ characters to obtain "RGBRGB" (same as with "GBRGBR" and "BRGBRG").

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

I tried to upsolve problem F. Once solved I submitted it. It failed on TC 3 so I spent some time trying to debug it but couldn't. I then read the editorial and it seemed that their approach was the same as mine. It would help me greatly if someone could tell me what was wrong with my solution.

57753983

Thanks in advance :)

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

    There are many things that are wrong starting with the order of Floyd Warshall. Your Infinity value is also low and your complexity is n^3 as you are not compressing the graph.

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

Question F was nice, but can someone provide an efficient method to solve the harder variant of this question, where k can take any value between 1 and m.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Problem E:

Can someone help me understand why BFS does not work? Something like an island trying to increase to all sides, or a minesweeper.

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

    Let $$$b = 2$$$ and $$$w = 7$$$ for these examples (swapping black and white gives the same result). In the bfs, start with an arbitrary black cell and spread to the white cells around it.

    image

    Say, in the bfs, you arbitrarily pick another black cell, next to one of the white cells that you've chosen.

    image

    By picking that cell, you restrict yourself to $$$w = 6$$$ and get the wrong answer (since the bfs solution would think it's impossible). This is because you are only able to add 2 additional white cells for the black cell. Always expanding rightward (or in any of the cardinal directions) allows you to always gain 3 white cells per black cell, which is the optimal structure (as proven above). Here's an example:

    image

    In this, it's clear to see that you can achieve the requirements of the test case. As another example, picking the 5 black cells in the center 3x3 square on that example board only allows you to get 12 white cells (much worse than the 5:16 ratio achievable with the optimal solution).

»
4 weeks ago, # |
Rev. 3   Vote: I like it -11 Vote: I do not like it

So, I got hacked due to a time limit exceeded on problem B. I followed a similar process as shown in the solution with an O(q*n) solution. But I think the if(k==1) break; statement would have optimized my solution.

57661799

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

    The complexity of editorial solution is actually $$$\sum\limits_{i = 1}^qn_i$$$ .

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

There is mistake in the solution to B: "at least one of k segments will have even number of odd elements (so will have odd sum)". An even number of odd elements yields an even sum, not an odd sum.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

For F I got "accepted" when I did not use the "main observation" from the editorial solution and just used lazy Dijkstra-s from every node run in parallel with one shared priority_queue so that I find shortest distances one by one until I find k of them. I needed to make Dijkstra-s lazy because otherwise I had "memory limit exceeded"--I think because my priority_queue became too big. Here is my commented code:

https://codeforces.com/contest/1196/submission/57786369

I still can't figure out its complexity. Could you please help me with this?

  • »
    »
    4 weeks ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    Your solution is almost identical with Geothermal's.

    You can read his unofficial tutorial. (link: https://codeforces.com/blog/entry/68642)

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

      Thanks, this is very helpful although I don't think he proved there the correctness of his complexity. I have left a comment regarding this under his tutorial. I hope I will find an answer.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Vovuh can you please tell me why the solution for D2 is always working ?

I am asking for the case when we have already a substring of size k in string s that is also the substring for the infinite string . Now when we are going to modify the string s according to offset then their are pretty much chances that the substring will be changed and we don't get optimal ans which is zero for this case .

I think you got my question ? Please explain ?

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

    You should read tutorial correctly.

    Vovuh are considering 3 different cases:

    1. We're going to change our substring of S, so that it starts on 'R' and after changes it will become substring of infinite string.
    2. We're going to change our substring of S, so that it starts on 'B' and after changes it will become substring of infinite string.
    3. We're going to change our substring of S, so that it starts on 'G' and after changes it will become substring of infinite string.

    Obviously, the answer on problem will be found.

»
4 weeks ago, # |
  Vote: I like it +5 Vote: I do not like it

Where can i find proof for problem F ?

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Please, help. In F, Can someone explain answer on test: 4 4 4 1 2 1 2 3 1 1 3 1 3 4 3

Author's solution gives 3 but optimal way is 1 -> 2 -> 3 with length 2.

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

    In the problem, you're not trying to find the $$$k$$$-th smallest path out of all paths, you're trying to find the $$$k$$$-th smallest shortest path. The path from 1 -> 2 -> 3 is longer than the path going directly from 1 -> 3, so it will not be counted because it's not a valid shortest path between two vertices. Rather, the path from 3 -> 4 of length 3 will be the next smallest shortest path after the ones with length 1.

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

I think some of the time limits are too restrictive for any solution in Python3 / PyPy to be accepted.

This is my solution for problem D2, where I am getting TLE just for I/O

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

The tutorial for problem E is quite misleading.

$$$w−1$$$ black cells to the up (all cells $$$(1,2w+2∗i)$$$ for all $$$i$$$ from $$$0$$$ to $$$w−1$$$)

should be $$$ w$$$ black cells <...> $$$(1, 2 w - 2 i) $$$

$$$w−1$$$ black cells to the down (all cells $$$(3,2w+2∗i)$$$ for all $$$i$$$ from $$$0$$$ to $$$w−1$$$)

$$$ w$$$<...> $$$(3, 2 w - 2 i) $$$

Do I misunderstand something?

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

i don,t know what the fuck is happening with me last one day . i m getting correct answer in my ide(atom as well as online compilers) but codeforces showing wrong answer.my sol id 57876850

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

1196B — Odd Sum Segments

for (int j = 0; j < n; ++j) { if (k == 1) break; if (a[j] % 2 == 1) { cout << j + 1 << " "; --k; } } cout << n << endl;

this snippet gets AC. why this gets WA? pls do explain -

for (int j = 0; j < n; ++j) {

       if (a[j] % 2 == 1) {
         cout << j + 1 << " ";

       }
    }
»
21 hour(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Vovuh the editorial has not been linked to the contest page. Please look into it.