### vovuh's blog

By vovuh, history, 8 months ago, ,

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

• +42

 » 8 months ago, # |   +43 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.
•  » » 8 months ago, # ^ |   0 cool!
 » 8 months ago, # |   +3 There's a typo, problem A links to 1015A instead of 1196A.
•  » » 8 months ago, # ^ |   +8 Thank you, fixed now!
 » 8 months ago, # |   +1 Wouldn't Floyd Warshall be too much for around 800 vertices?
•  » » 8 months ago, # ^ |   +9 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 :(
•  » » 8 months ago, # ^ |   +20 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 :)
•  » » » 8 months ago, # ^ | ← Rev. 2 →   +3 Yeah well I didn't even come up with the Floyd Warshall idea during contest so t.t
•  » » 8 months ago, # ^ |   +5 Just use dijkstra on all vertices. That gives O(n*m*log(n)) solution.
•  » » » 8 months ago, # ^ |   +3 Will it be within Time limit? (Considering n and m are in the range of 10^5).
•  » » » » 8 months ago, # ^ |   +8 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)).
•  » » » » » 8 months ago, # ^ |   +3 Okay, got it. :)
 » 8 months ago, # |   0 Great editorial! All the solutions are very nicely explained.
 » 8 months ago, # | ← Rev. 2 →   0 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.
•  » » 8 months ago, # ^ |   0 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.
•  » » » 8 months ago, # ^ |   0 (cnt-k+1)%2 == 1**, and with some manipulation you can get the cnt%2 == k%2
•  » » » » 8 months ago, # ^ |   0 Can you please show the manipulation to get cnt%2==k%2
•  » » » 8 months ago, # ^ |   0 Thank you very much, I understood the idea.
•  » » » 8 months ago, # ^ | ← Rev. 2 →   0 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.
•  » » » » 8 months ago, # ^ | ← Rev. 2 →   0 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.
•  » » » » » 8 months ago, # ^ |   0 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)
•  » » » » » » 8 months ago, # ^ |   0 Thank you very much. You helped me a lot.
•  » » 8 months ago, # ^ |   0 i think so!
•  » » 8 months ago, # ^ |   0 This snippet creepily matches with mine 57687430
•  » » » 8 months ago, # ^ |   0 :D,really amazing
•  » » 8 months ago, # ^ |   0 I did the same :D
 » 8 months ago, # |   +1 The best Div 3 ever ! Fast and quality
 » 8 months ago, # |   0 Why do we need only min(k,m) edges for problem F?
•  » » 8 months ago, # ^ |   +6 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.
 » 8 months ago, # |   0 why there are two Tutorials
•  » » 8 months ago, # ^ |   0 Two authors
•  » » 8 months ago, # ^ |   0 Official and unofficial
 » 8 months ago, # |   0 The solution to E is very beautiful
 » 8 months ago, # |   0 I don't understand why doesn't need more than min(k,m), somebody knows the reason for this it
•  » » 8 months ago, # ^ |   0 Explained above.
 » 8 months ago, # | ← Rev. 2 →   +1 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.
•  » » 8 months ago, # ^ |   0 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)
•  » » » 8 months ago, # ^ |   +3 Could you please explain this solution more? And why does it have this complexity?
•  » » » » 8 months ago, # ^ |   +1 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)
•  » » » » » 8 months ago, # ^ |   +3 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!
 » 8 months ago, # |   +3 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 :DWant moar!
 » 8 months ago, # |   0 I wrote n dijkstra's algorithms in F. I suppose it works in $O(nklog(n))$. It still passed in 450ms.
 » 8 months ago, # | ← Rev. 2 →   0 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
•  » » 8 months ago, # ^ |   0 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").
•  » » » 8 months ago, # ^ |   0 thanks alot!
 » 8 months ago, # |   0 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.57753983Thanks in advance :)
•  » » 8 months ago, # ^ |   +1 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.
•  » » » 8 months ago, # ^ |   +4 Oh, oof.Thanks though :)
 » 8 months ago, # |   +1 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.
•  » » 8 months ago, # ^ |   0 Geothermal's tutorial is considering another solution with complexity O((N+K)log(N+K)+MlogM).
 » 8 months ago, # |   0 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.
•  » » 8 months ago, # ^ |   0 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. imageSay, in the bfs, you arbitrarily pick another black cell, next to one of the white cells that you've chosen. imageBy 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: imageIn 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).
•  » » » 8 months ago, # ^ |   0 Thank you for your explanation :)
 » 8 months ago, # | ← Rev. 3 →   -11 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
•  » » 8 months ago, # ^ | ← Rev. 4 →   0 The complexity of editorial solution is actually $\sum\limits_{i = 1}^qn_i$ .
 » 8 months ago, # |   0 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.
 » 8 months ago, # |   0 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/57786369I still can't figure out its complexity. Could you please help me with this?
•  » » 8 months ago, # ^ | ← Rev. 2 →   +1 Your solution is almost identical with Geothermal's.You can read his unofficial tutorial. (link: https://codeforces.com/blog/entry/68642)
•  » » » 8 months ago, # ^ |   0 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.
 » 8 months ago, # |   0 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 ?
•  » » 8 months ago, # ^ |   0 You should read tutorial correctly.Vovuh are considering 3 different cases: 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. 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. 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.
 » 8 months ago, # |   +5 Where can i find proof for problem F ?
 » 8 months ago, # |   0 Please, help. In F, Can someone explain answer on test: 4 4 4 1 2 1 2 3 1 1 3 1 3 4 3Author's solution gives 3 but optimal way is 1 -> 2 -> 3 with length 2.
•  » » 8 months ago, # ^ |   +1 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.
•  » » » 8 months ago, # ^ |   0 Thank you very much!
 » 8 months ago, # |   0 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
 » 8 months ago, # |   0 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?
 » 8 months ago, # |   0 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
 » 8 months ago, # |   0 1196B — Odd Sum Segmentsfor (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 << " "; } } 
 » 7 months ago, # |   0 Vovuh the editorial has not been linked to the contest page. Please look into it.
 » 6 months ago, # |   0 not getting the flaw in my code for the problem "RGB Substring (easy version)". My code is passing the sample test cases But showing wrong verdict on test case 2. Can anyone plz help?
 » 5 months ago, # |   0 Hi! I don't understand why my code in python 3 do not works in the problem A — Three Piles of Candiesfor a in range(int(input())): print(int((sum([int(i) for i in input().split()]))/2))It says wrong answer on the third test....
•  » » 5 months ago, # ^ |   0 you just need to do floor division by using '//' operator.for a in range(int(input())): print(int((sum([int(i) for i in input().split()]))//2))
 » 2 months ago, # |   0 for A if the case is 1 4 1 then say alice has 1 and bob has 4 then the the last pile if alice takes it completely, the answer should be 2 but according to editorial it is 3. can some one explain!
 » 2 months ago, # |   0 Talking about problem B!I am actually trying to get a firm grip on question 'B'. In cases where the answer is a "Yes", what thing do I exactly need to print in the following line? An explanation accompanied by an example would be appreciated. Thanks in advance!
 » 2 weeks ago, # | ← Rev. 2 →   0 Hi I am trying to solve 1196C — Robot Breakout problem and here is my solution. I don't understand what I did wrong but for test case five it is giving wrong answer with the log wrong answer Jury has the answer for query 294 but participant has not. Please help me find out mistake in my solution, thank you.