### Kostroma's blog

By Kostroma, 3 years ago, translation, ,
Code
Code
Code
Code
Code
Code
Code

•
• +79
•

 » 3 years ago, # |   +4 Hi, I am not able to understand the string generation part of Recover string problem div2 D. Please explain the intution and other insight for the problem.Thanks All
•  » » 3 years ago, # ^ |   +2 I rewrote editorial, please check.
•  » » » 3 years ago, # ^ |   0 Hi zeliboba,Please explain what does this mean {a00, a01 + a10, 0, a11}? I am having trouble in understanding the swap. I have tried to do swap for initial string b = 0001111. I got stuck.
•  » » » » 3 years ago, # ^ |   0 Thanks zeliboba I got it now.
•  » » 3 years ago, # ^ |   +1 Mine solution is a bit different from the bubble sorting which the editorial provided, but I hope this helps.Consider that for each '1's in the string generated, the amount of sub-sequences of "10" that contains this '1' equals to the amount of '0' after it. By this fact, we can build a greedy solution and determine if '1' or '0' should be put on position i.Case 1: There are more unused '0's than "10" that are yet to be built -> put '0' here since '1' will not fulfill the situation.Case 2: The counterpart of case 1 -> put '1' here and it won't violate the situation.This works because we have checked that cnt(1) * cnt(0) = cnt(01) + cnt(10), and this greedy solution can always get a result string of exactly c sub-sequences of "10".The time complexity is O(n).
•  » » » 3 years ago, # ^ |   0 The same idea is described in the end of editorial for this problem
•  » » 3 years ago, # ^ | ← Rev. 2 →   +6 For the linear time solution approach try considering the following example:Suppose given input of : 1 4 2 3Then number of 0's=2And number of 1's=3Clearly there will be a solution ( (4+2)==2*3 )Then we are moving from the string of 00111 to the desired string as follows (in braces follows the aii's):00111 {1,6,0,3}Shift one zero to end01110 {1,3,3,3}Now shifting the other zero will not lead to ans as a01 which is 3 now will only decrease.Then move the 0 just moved to the left by the amount needed to equate it to the a01 given in the input ( 4-3=1)Moving that 0 left gives the desired ans01101 {1,4,2,3}
•  » » » 3 years ago, # ^ |   0 Thanks a lot!! dsharma080.
•  » » » 3 years ago, # ^ | ← Rev. 3 →   0 There is also online algorithm.Suppose, we know the string should have n zeros and m ones. It means the total length should be n + m.We will create our string one character at a time from left to right. At each position we have a choice — either we put 0 or we put 1. Let's start from the first position.If we place 0 as the first character, we will have m pairs of the type 01 (because we will have to put m ones after the current zero in the future). If we place 1 as the first character, we will have n pairs of the type 10.At i'th position we know that we should have a01 and a10 pairs of appropriate type. Placing 0 at current position inevitably leads to generating some amount of pairs of type 01. If this amount is greater than a01, it means that we cannot place 0 in the current position.This logic leads to the following code. while (n + m) { if (a01 >= m) { cout << 0; a01 -= m; n--; } else { cout << 1; a10 -= n; m--; } } 
•  » » » » 14 months ago, # ^ |   0 Thanks a lot for your explanation and code egor.okhterov. I was stuck on this question for a while.
 » 3 years ago, # |   0 I am lost in the prove of Div1C... Could someone explain the prove a bit less formal?
•  » » 3 years ago, # ^ |   +31 Here's a simpler explanation for Div.1 C :Fix the centroid as the root of the tree (can be found in O(n)). Now, for a given vertex x, we need to see if it can be a centroid by replacing an edge. Let the root be r and the subtrees be S1, S2, ..., Sk. The key is to note that if we remove x, the components in the subtree of x is definitely small (i.e. at most ), since r is the centroid. Also, the link from it to its parent is removed. Also, an edge replacement will clearly remove an edge from the remaining tree (the tree obtained after removing x's subtree) and replace it with an edge connecting to x (otherwise it doesn't change anything). Thus, when x is removed, the newly placed edge is removed as well. This means that we only need to find the edge to remove in the remaining tree to minimize the maximum size of the two resulting components. Also, this can be thought as taking a subtree out of the remaining tree. Since the subtree size is at most (r is the centroid), we need to maximize the size of the subtree we take, which does not contain r. Now, it is clear that we'll remove one of S1, S2, ..., Sk and we can easily calculate the answer by storing the two maximum subtree sizes among these subtrees.Code : 20156041
•  » » » 3 years ago, # ^ |   +8 Great solution!
•  » » » 3 years ago, # ^ |   0 Your solution definitely seems to be easier to understand then the one given in editorial. But still I am unable to understand the crux of it. I get the solution till the part where you have removed x from the tree and link of par[x] and x is removed. After that I don't get the idea where you have placed the new edge. Can you please help me out here?
•  » » » » 3 years ago, # ^ |   0 After you remove the link between par[x] and x, the graph effectively split into few trees, those that are "below" x and the big tree that contains the original root. The trees "below" x are small, since the root is the centroid. So, we need to only change the size of the big tree. Thus, it only makes sense to remove an edge from the big tree. Where do we add the extra edge? We just add it such that one of its endpoints is x, so when x is removed this edge disappears as well. Now, we are only left with the problem in determining which edge from the big tree to remove, which is simple.
•  » » » 3 years ago, # ^ |   0 Thanks for the help, now I get how it works. =)
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 Regarding Div.1 C, I have 1 query. Why is it that only one of the subtrees of the original centroid(root) will be disconnected ? why cannot centroid be included in the removed portion ? I cant seem to prove this fact on my own. Pls help!
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 can u explain case when bestidx == -2 comes into play in your solution ? ie when sub[i] == best !
•  » » » 3 years ago, # ^ |   0 plz find bug in my code :: !! http://codeforces.com/contest/709/submission/20545506
 » 3 years ago, # |   0 Even though I kept getting WA on case n = 1, there's "a don't think" way of solving Problem B using DP.Run DP from left, and from right. Choose a point you start at,and depending on your direction keep multiplying the transitions by 2. Once you choose your point, don't, and just go forward/backward normally.You should also take care of that you skip at most one item using a flag.Not the best solution, but an easy way to solve it.
 » 3 years ago, # | ← Rev. 2 →   +25 can someone please explain how to solve div 1 D ? or is there a general method to solve this kind of flows ?
•  » » 3 years ago, # ^ |   +5 Consider the case where ci = ∞ and there are no edges directed from or to the source and the sink. Let ini and outi be the total amount of input flow (flows towards vertex) and output flow (flows from vertex) of vertex i respectively. Let di be outi - ini, then we want to make di = 0 for all i.Build a costed flow graph with n + 2 vertex including a new source and sink, and the n representative of the vertices in the original graph. For every i, if di > 0, then construct an edge from source to i with cost 0 and flow di. Otherwise, construct an edge from i to sink with cost 0 and flow  - di.Consider every edge in the original graph from u to v with flow f, Build an edge from u to v with cost 1 and flow ∞ in the costed flow graph. Build an edge from v to u with cost 1 and flow f in the costed flow graph. Compute the min-cost-max-flow of the costed flow graph, and the minimum cost is the cost required to fix the flow.Reasoning is not hard after the graph is built, but as my explanation skill is horrible I tend to skip this part. After you see the reasoning, the handling of ci and source and sink should be trivial. (Though I overcomplicated a lot of things and got WA... TAT)
•  » » » 3 years ago, # ^ |   0 cool, I've already found the tut in icpc-camp blog, great thanks all the same. wow, classical flow problem, feasible flow, and based on it, this problem will be solved.
•  » » » » 17 months ago, # ^ |   0 can u plz mention the link of the tutorial
•  » » » » » 17 months ago, # ^ | ← Rev. 4 →   0 sorry for replying late.there is only a tutorial in Chinese. original(I don't know why I can't post the link in the right way
•  » » » » » » 17 months ago, # ^ |   0 cool
 » 3 years ago, # |   0 In 709B, shouldn't we also pay attention for when we don't have to return to the point at the other side? For example, when the entry is: 2 5 2 8 We should only go to the leftmost checkpoint, but we shouldn't return to the rightmost checkpoint in order to pass through the right checkpoints because the only point that is to the right is the one we chose not to visit.
•  » » 3 years ago, # ^ |   0 If you are not going to go to 8, then 2 is both leftmost and rightmost, and you need to go to it, and then to it again, but the second one cost you nothing
•  » » » 3 years ago, # ^ |   0 Bad example, sorry. What about 3 5 3 4 10 4 is the rightmost (ignoring 10, obviously), but there's no need to go to it since I've already passed through it
•  » » » » 3 years ago, # ^ |   0 Then you go to the right first and to the left then
•  » » » » » 3 years ago, # ^ |   0 I see, thanks.
 » 3 years ago, # |   0 Div1C: (assume its root is w), remove an edge uw between its subtree and the remaining tree, and add an edge between x and u I guess we need to add edge between x and w (since subtree is rooted at w and is detached).
•  » » 3 years ago, # ^ |   0 Thank you, it will be fixed as long as codeforces manage to take the new version of editorial from polygon.
 » 3 years ago, # |   +8 In the main post you mentioned cheaters. What exactly happened?
•  » » 3 years ago, # ^ |   +12 It happens from time to time, some guys send equal solutions for the problems and after the contest they are banned.
 » 3 years ago, # |   0 I think that the problem with problem A is that people did not realize that the "waste" is actually the juice and not the oranges you throw out. That is why I couldn't understand it at first.
 » 3 years ago, # |   0 Can anyone see this code for the strings question and tell me what's wrong with it? http://ideone.com/sFgcXB
•  » » 3 years ago, # ^ |   0 When the string only has a's you are not doing anything, when you should. The problem says that we must find one non-empty substring and make the changes asked. So when you have "aaaa" for example, you must shift the last letter. In this case the output would be "aaaz".
•  » » » 3 years ago, # ^ |   0 I did that and now its giving TLE on test 27. Could you tell me what modification needs to be done?
•  » » » » 3 years ago, # ^ |   0 A greedy algorithm works in this problem, so you can simply iterate through the letters, making the changes, until you find an "a". When you find it, break.Now if you've made changes, print the string. If you haven't, shift the last letter. You don't really have to do all these ifs
•  » » » » 3 years ago, # ^ |   0 It is because of the repeated string comparisons. The idea is very easy to implement, it shouldn't take that many lines of code. http://codeforces.com/contest/709/submission/20125058
 » 3 years ago, # |   +15 That may be a very stupid challenge, but it is also possible to solve Div1E for constraints like m ≤ 10, n ≤ 109 :P
•  » » 3 years ago, # ^ |   +5 Yeah, we also thought about it =)
 » 3 years ago, # |   0 In the last paragraph for Div1E, it seems you dropped the t - 1 index from sum_dpr[t - 1][n]. It might be a bit obvious, but it confused me the first time I read it.
•  » » 3 years ago, # ^ |   0 Thank you, it is fixed now.
 » 3 years ago, # | ← Rev. 2 →   0 Hi. What is cnt array in Div1C (Centroids)? I have never heard this shortcut.
 » 3 years ago, # |   0 For div1 E,what's the a/b-the probability mean? Isn't it the probability that each block not been destroyed?
 » 3 years ago, # |   +5 Where is the editorial for problem D?
•  » » 3 years ago, # ^ |   -8 At the top of the editorial it says: Here is the part of the editorial, other problems will come a bit later 
•  » » 3 years ago, # ^ |   0 It is already here btw
•  » » » 3 years ago, # ^ |   0 Are you telling me that if I code exactly what you said in editorial for problem D it will get AC? OK I will believe you.
 » 3 years ago, # | ← Rev. 2 →   0 ignore
 » 3 years ago, # |   0 hello ! for div2 B , i got wa on test 9, and i dont know where the errors are , here is my code 20193727 , can any one give me some advice about my algorithm , thanks! ps.sorry for my poor english TAT.
 » 3 years ago, # |   0 In the editorial for C shouldn't the -1 in the inequality be +1?
 » 3 years ago, # |   0 For problem Div2 D (Div1 B):"If a01 + a10 ≠ c0·c1, then answer is impossible."How do you get this formula? What does it really mean?Thanks
•  » » 3 years ago, # ^ |   +3 You have c0 zeroes and c1 1's. number of ways of choosing one 0 and one 1 are (c0 choose 1)*(c1 choose 1) which is c0*c1. Now all such pairs will either be contributing to a01(if the zero you chose came before the one you chose) or a10(if the one you chose came before the zero you chose) . There is no other possibility. Hence a01 + a10 = c0.c1.
 » 3 years ago, # |   0 I can't get the point of problem C. When we remove the point which we want to turn into a centroid, do the incident edges get removed too?
•  » » 3 years ago, # ^ |   0 Yes. This splits you tree in several connected components.
 » 3 years ago, # |   0 Hi, I always get TLE on test 21... I passed the test point which n = 400000 but I can't pass the 399999 one lolIs it random? I use bfs instead to avoid stackoverflow.
 » 2 years ago, # |   0 Why this tutorial is not attached as the contest materials in the contest page?
•  » » 17 months ago, # ^ |   0 I've tried my best, but cf server didn't let me do this.
•  » » » 17 months ago, # ^ |   0 Oh! Probably MikeMirzayanov can do something..