### awoo's blog

By awoo, history, 22 months ago, translation,

1680A - Минимумы и максимумы

Idea: BledDest

Tutorial
Solution (BledDest)

1680B - Роботы

Idea: BledDest

Tutorial
Solution (BledDest)

1680C - Двоичная строка

Idea: BledDest

Tutorial
Solution (BledDest)

1680D - Прогулка с собакой

Idea: vovuh and BledDest

Tutorial
Solution (vovuh)

1680E - Двигающиеся фигуры

Idea: vovuh

Tutorial
Solution (vovuh)

1680F - Свободное вершинное покрытие

Idea: BledDest

Tutorial
Solution (awoo)
• +45

| Write comment?
 » 22 months ago, # |   +3 Can someone share his sliding window approach for problem C ?
•  » » 22 months ago, # ^ |   0 Reduce the problem: find the lowest cost substring, cost = max(0's, total 1's — 1's)The cost of the final optimal substring will either be contributed by 0's or by 1's. Let's binary search on the best answer that can be contributed by 0's and then binary search again on 1's, then we take the minimum of the two. To check if an answer g is possible, we run sliding window. For example, let's say we are currently binary searching on best answer contributed by 0's. We can expand the window until we have more than g 0's in the current window. Then we can shrink it from the left until we have exactly g 0's again. here's my submission: https://codeforces.com/contest/1680/submission/157099290
•  » » » 20 months ago, # ^ |   0 No need to check for 0's, we can show that if some cost k is achieved we can achieve it using the number of 1's removed = k (and hence the number of 0's left <= k). The proof of this is, suppose on the contrary we get a score of k and remove less than k 1's, then the number of 0's left in the string is k. Now we should not be able to remove more 0's from the string, or we will get a lower score. Hence there should be sufficient 1's surrounding the 0's so that we can bring the count of removed 1's up to k.
•  » » 22 months ago, # ^ |   0 Sliding window / two pointers approach without binary search, runs in O(n) time, in C. Let our window represent the string left after deleting the first i elements, and the size - j elements from the right. We know that for a window starting at index i, if we're increasing the window size, then the best cost we can get is when the number of characters 0 in our window is equal to characters 1 outside the window. This is because if we keep increasing our window size then the number of character 0 in the string will keep increasing, and a smaller window will increase the number of character 1 outside the string. Then we just iterate through i, the beginning of each window, and keep the maximum window we can get. When i goes to the next iteration, it will find the next possible window starting at the previous i's max (to keep it O(n)), while also keeping track of the minimum cost as well.
•  » » » 22 months ago, # ^ |   0 Great approach !! Understood completely. Thx
•  » » » 3 months ago, # ^ |   0 Isn't is O(n^2) solution
•  » » 15 months ago, # ^ |   0 We know that the cost is max(Oremoved,Zleft) here Oremoved = totalOne-Oleft. Also Oleft and Zleft are related as Oleft+Zleft = len, where len is size of substring. Now cost is max(totalOne-Oleft,Zleft) = max(totalOne-len+Zleft,Zleft). One can clearly see now that if len>=totalOne , then cost is equal to Zleft. So for len>=totalOne we would want to keep the length of substring minimum as increasing len can only increase the number of zeros(Zleft) in the substring. So we check only those substrings for which len = totalOne. for the case where len
 » 22 months ago, # |   +1 Can someone share all the approaches to question C as mentioned in the tutorial? It will be nice to see them all at once and compare their complexities Dynamic programming, Greedy, Two pointers
 » 22 months ago, # |   0 Has anyone solved problem C with dynamic programming？
•  » » 22 months ago, # ^ |   0 dynmaic programming(prefix sum) - Submission
•  » » » 22 months ago, # ^ |   0 Can you please elaborate your solution. Precisely what the last for loop means.
•  » » » » 22 months ago, # ^ |   0 Refer to this COMMENT for explanation.
 » 22 months ago, # | ← Rev. 2 →   +3 Can C be solved using ternary search? If yes, can someone share their solution for the same?
•  » » 22 months ago, # ^ | ← Rev. 3 →   0 Here is my solution with ternary search.157125272I think the solution can be improved and may be done more clearly.
•  » » » 21 month(s) ago, # ^ |   0 I also tried it using ternary search but got WA as verdict , can you help me finding the test case where it is failing ? 157066350 For every index j in the string , I am finding the optimal i so that cost is minimum using ternary search .
 » 22 months ago, # |   +3 Binary Search Video Solution for C
 » 22 months ago, # |   +3 green !
 » 22 months ago, # |   0 I used a segment tree.I think I can replace it by a monotune queue.
 » 22 months ago, # |   +31 Problem F looks very similar to https://codeforces.com/contest/19/problem/E
 » 22 months ago, # |   +11 For problem E: a "state machine" approach works, without any DP.The idea: same as in the editorial, we sweep all the chips from left to right. If we reach a column with a single chip, just move it to the right and add 1 to the answer. What if we reach a column with 2 chips (whether it's 2 chips that are already there, or 1 chip is there and another has been pushed in from the left)? In this case, we push the top chip down or the bottom chip up. But we don't have to consider both possibilities yet. Just put the chip in an "indeterminate" state and resolve the state when you reach a column with exactly one chip.Of course, this doesn't save much in terms of runtime/space — the dp solution only carries around 2 integers. But I think it's a nice way of looking at the problem.
 » 22 months ago, # |   0 Hey, can anyone help me with problem C. I've thought of a greedy solution whose basic idea was to remove the max number of zeros by removing min number of ones and repeat this till the string becomes empty from zeros.I tried checking with many edge cases but cannot see what is the mistake ive made. So plz can anyone help me I have commented the code for better understanding https://ideone.com/bBoj0b
•  » » 22 months ago, # ^ |   +1 Failing testcase: Ticket 7173
 » 22 months ago, # |   0 I think there is a typo in the editorial of problem E, because the transition $dp_{i, 1} \rightarrow dp_{i+1, 1}$ is considered twice. It's not really important though because it's easy to understand that the last transition is meant to be $dp_{i, 1} \rightarrow dp_{i+1, 0}$
 » 22 months ago, # |   +2 If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints. A: Minimums and Maximums B: Robots C: Binary String D: Dog Walking E: Moving Chips F: Lenient Vertex Cover: Will add if anyone's stuck on debugging (only till the next 7 days). If you are not able to find a counter example even after changing the parameters, reply to this thread (only till the next 7 days), with links to your submission and ticket(s).
 » 22 months ago, # |   -11 Finally an expert after this contest, really liked third problem
 » 21 month(s) ago, # | ← Rev. 4 →   0 Problem F is very nice.The final part can also be done with LCA. If you want to remove an edge in the tree, which splits it into two subtrees, the edge needs to be so the bad edges not in the original tree (ie. those non-tree edges that connect vertices of the same color) need to be in separate subtrees so we can flip the colors of one subtree.Then the problem becomes: Given several paths on a tree, find an edge that lies on all of them. This will be the edge we remove. The intersection of two paths is empty, a vertex, or a subpath. It can be checked with a few cases using LCA whether the intersection is another path, and we repeat checking with this newer subpath.
•  » » 21 month(s) ago, # ^ |   0 Hey, do you think you could help me understand solution to problem F? I understand when it says we need to remove one edge such that the remaining graph is bipartite but I don't after that I don't really understand anything about how we find this edge. Thanks.
 » 21 month(s) ago, # | ← Rev. 2 →   +1 Can anyone help finding which TC my submission fails on? https://codeforces.com/contest/1680/submission/157535516Edit: Problem E
•  » » 21 month(s) ago, # ^ |   +1 I did the same! IDK where I am going wrong. If you get it do tell me.
 » 21 month(s) ago, # |   0 Hello, BARBARIANNNNN , can you please explain your implementation of 157031361, i.e. problem 1680C - Двоичная строка , i am not getting a proper intuition of your code. And can you please tell me this condition : while(j<=i && c0>x) { } // my doubt is why we are only comparing c0 with x and not change the loop like this while(j <=i && c1 > x) { } if(c0 <= x && c1 <= x) return true; changing this is actually giving the wrong answer and giving the bigger cost . Why is it so as we are also considering all the cases in comparing c1 with x ?
•  » » 20 months ago, # ^ | ← Rev. 2 →   +3 In the function check, we use $[i,j]$ mention the subsegment after our operations. Because we want the max of the number of characters 0 left in the string and the number of characters 1 removed from the string to be $\le x$, we should control the c0(the first number in the condition) $\le x$, while we check whether c1(means the second number in the condition) also $\le x$. If we change the loop like while(j <=i && c1 > x), then when you add j, the c1 will increase, and it is meanless(never c1<=x because of increasing).
•  » » » 20 months ago, # ^ |   +8 Thank you very much BARBARIANNNNN for taking out your time and expalining it clearly. Actually, TBH as a newbie i do not expect so much replies from high rated coders as i am a newbie. But when someone takes out time to resolve my doubt, it just increases my enthusiasm , makes me happy and fills me with a new vigor to practice more. Really, Thank you again for your time to resolve my doubt BARBARIANNNNN as i have been actually stuck for a long time trying to understand the intuition of your implementation.
 » 18 months ago, # |   0 I'm unable to understand editorial of problem C. Can someone explain it clearly?
•  » » 16 months ago, # ^ | ← Rev. 2 →   0 Well, we binary search the number of how much '1's did we delete. Suppose, we have M as the number of deleted '1's. Than we need to find the range with the shortest length of remaining '1's (That's why we need to make an array named pos, where we conserve positions of i-th '1', before binary search). Let's make a loop where we delete first i '1's and last m — i '1's. In this loop we find the shortest length between remaining '1's. After this loop, if the number of '0' in this min-length range of remaining '1's is less then number of deleted '1's, than it's a good answer and we can check lesser answers, Otherwise it's a bad answer and we move to the right. 180966441 — here's my solution
•  » » » 16 months ago, # ^ |   +1 Thank you so much
 » 12 months ago, # |   0 Hey can someone give me a counter example for this submission of E: https://codeforces.com/contest/1680/submission/197569270
•  » » 12 months ago, # ^ | ← Rev. 2 →   0 nevermind i got it:s1 = '*.***'s2 = '***.*'
 » 11 months ago, # | ← Rev. 2 →   0 deleted