vovuh's blog

By vovuh, history, 5 years 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

| Write comment?
»
5 years 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.

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

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

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

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

  • »
    »
    5 years 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 :(

  • »
    »
    5 years 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 :)

    • »
      »
      »
      5 years 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

  • »
    »
    5 years 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.

    • »
      »
      »
      5 years 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).

      • »
        »
        »
        »
        5 years 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)).

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

Great editorial! All the solutions are very nicely explained.

»
5 years 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.

  • »
    »
    5 years 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.

    • »
      »
      »
      5 years 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

    • »
      »
      »
      5 years 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.

      • »
        »
        »
        »
        5 years 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.

        • »
          »
          »
          »
          »
          5 years 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)

»
5 years 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]);

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

The best Div 3 ever ! Fast and quality

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

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

  • »
    »
    5 years 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.

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

why there are two Tutorials

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

The solution to E is very beautiful

»
5 years 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

»
5 years 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.

  • »
    »
    5 years 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)

    • »
      »
      »
      5 years 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?

      • »
        »
        »
        »
        5 years 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)

        • »
          »
          »
          »
          »
          5 years 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!

»
5 years 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!

»
5 years 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.

»
5 years 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

  • »
    »
    5 years 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 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I know its an old thread but I hope you would help, I wrote the solution of D2 by making three offset string RGB... GBR... BRG... and comparing them with original s.

      But I'm getting this unusual error of uninitialized value usage in my solution of 4th test case, cf online judge is highlighting the line but I'm unable to crack it. Can you please help?

      Here's my Submission

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

        I see this issue immediately: memset(g,0,sizeof(b));, where I guess you intend to memset on b instead?

        Try fixing that, if it still doesn't work, message back probably

»
5 years 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 :)

  • »
    »
    5 years 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.

»
5 years 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.

»
5 years 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.

  • »
    »
    5 years 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).

»
5 years 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

  • »
    »
    5 years 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$$$ .

»
5 years 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.

»
5 years 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://mirror.codeforces.com/contest/1196/submission/57786369

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

  • »
    »
    5 years 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://mirror.codeforces.com/blog/entry/68642)

    • »
      »
      »
      5 years 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.

»
5 years 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 ?

  • »
    »
    5 years 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.

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

Where can i find proof for problem F ?

»
5 years 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.

  • »
    »
    5 years 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.

»
5 years 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?

»
5 years 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.

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

Hi! I don't understand why my code in python 3 do not works in the problem A — Three Piles of Candies

for a in range(int(input())): print(int((sum([int(i) for i in input().split()]))/2))

It says wrong answer on the third test....

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

Can somebody please explain the solution to problem d2

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

I coded the same idea for Problem F, but thought there might be a problem with it while coding. I got accepted anyways (and had a good moment of laughter bc i expected either TLE or WA) but now I'm here to ask, what if the weights of all the edges falls below the 400th minimum of all weights, so we're left with the very given graph... no reduction is made. How does this not face TLE?