

Educational Codeforces Round 128 Editorial
source link: http://codeforces.com/blog/entry/102852
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

4 months ago, # | Can someone share his sliding window approach for problem C ? |
-
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
-
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.
-
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 thesize - 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 characters0
in our window is equal to characters1
outside the window. This is because if we keep increasing our window size then the number of character0
in the string will keep increasing, and a smaller window will increase the number of character1
outside the string.Then we just iterate through
i
, the beginning of each window, and keep the maximum window we can get. Wheni
goes to the next iteration, it will find the next possible window starting at the previousi
's max (to keep it O(n)), while also keeping track of the minimum cost as well.Submission: https://codeforces.com/contest/1680/submission/157038960
4 months ago, # | 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 |
4 months ago, # | Has anyone solved problem C with dynamic programming? |
Can C be solved using ternary search? If yes, can someone share their solution for the same? |
-
Here is my solution with ternary search.
157125272
I think the solution can be improved and may be done more clearly.
4 months ago, # | green ! |
4 months ago, # | I used a segment tree.I think I can replace it by a monotune queue. |
4 months ago, # | 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. |
4 months ago, # | 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 |
4 months ago, # | I think there is a typo in the editorial of problem E, because the transition dpi,1→dpi+1,1dpi,1→dpi+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 dpi,1→dpi+1,0dpi,1→dpi+1,0 |
4 months ago, # | 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.
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). |
4 months ago, # | Finally an expert after this contest, really liked third problem |
i really want to solve the problem c by myself but i am stuck for a few days. can anyone give me any insight to approach the problem |
4 months ago, # | In problem C, I iterate to [0,n-1] taking only the 'i'th character at each iteration as the first character of the substring, and for finding the last position of the substring, use the binary search. so how do we binary search it! first, compute the prefix of both '0's and '1's in the string, then use those prefixes to compute the max('0's present in the substring, '1's deleted from the substring). if('0's present in the substring > '1's deleted from the substring) then shift left=mid+1; else, shift right to mid. And then compute the minimum of answer and the substring computed up to left. https://codeforces.com/contest/1680/submission/157425332 |
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. |
I don't understand how the dog walks in the second example of question D |
Can anyone help finding which TC my submission fails on? https://codeforces.com/contest/1680/submission/157535516 Edit: Problem E |
I found a different simple solution for D. Let p[i]=∑k≤ia[i]p[i]=∑k≤ia[i] be where the dog is after ii steps. Let pre[i]pre[i] denote sum of nonzero (known) steps in the first ii steps. Let cnt[i]cnt[i] denote the number of zeros in the first ii steps. The problem is equivalent to maximizing maxip[i]−minip[i].maxip[i]−minip[i]. Thus, for each pair of indices (i,j)(i,j) we check if we can make p[i]p[i] the min and p[j]p[j] the max or vice versa, and what optimal difference we can get with this pair. (Clearly if a[i]>0a[i]>0 then it cannot be the min so we can ignore such cases, etc.) We need the following inequalities to hold:
where the first two inequalities are obvious, the third inequality ensures that the dog can return to zero after reaching the maximum at j,j, and the fourth inequality is that the dog can go from the min at ii to the max at j.j. It can easily be checked whether these four inequalities hold, and find the optimal difference for this pair (i,j).(i,j). The overall runtime is O(n2).O(n2). |
4 months ago, # | Here's a funny solution to E using graphs. Form a graph where vertices are the chips. Initially we can connect all pairs of edges between the vertices, where distance between two vertices is the manhattan distance. Then the answer is just given by the MST of the graph: Moving chip i→ji→j corresponds to taking that edge. Finally, notice that for any chip, we only need to consider edges from that chip to its (at most) four nearest neighbors. The ones closest to the left and right of it in the same row, and similarly for the other row. This is because if we move the chip to any of the other chips in a move, we can improve this by first moving to one of these four chips along the way and the number of moves is the same. Thus the number of edges in the graph is O(n),O(n), so MST can be done in O(nlogn).O(nlogn). |
-
Unfortunately, the MST is not the answer because we can combine chips at intermediary positions. Intuitively, a counterexample in euclidean space would be a triangle (the sides of the triangle would be the MST but we can move the chips to the middle).
A counterexample in the context of the problem would be:
.*.*.
..*.*
The MST would take 6 moves, but they can be combined in 5.
3 months ago, # | Hello, BARBARIANNNNN , can you please explain your implementation of 157031361, i.e. problem 1680C - Binary String , 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 ? |
-
In the function check, we use [i,j][i,j] mention the subsegment after our operations. Because we want the max of
the number of characters 0 left in the string
andthe number of characters 1 removed from the string
to be ≤x≤x, we should control the c0(the first number in the condition) ≤x≤x, while we check whether c1(means the second number in the condition) also ≤x≤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).-
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.
-
12 days ago, # | I'm unable to understand editorial of problem C. Can someone explain it clearly? |
vovuh awoo BledDest Is it not possible to solve problem E using Binary Search + BFS ? we can binary search on the number of moves allowed, if possible to collect all chips with x moves then always possible to collect with x+1 moves. And for a given number of moves (x) we can start a BFS from any chip and check if possible to collect all or not. Since, given number of moves most optimal way would be to move a single chip x times. Please let me know if there is any flaw in the logic. I will try to code this solution and submit once. Just want to know if there is a flaw where am I making a mistake. |
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK