Kostroma's blog

By Kostroma, 8 years ago, translation, In English
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
  • Vote: I like it
  • +79
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it +4 Vote: I do not like it

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

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    I rewrote editorial, please check.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    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).

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

      The same idea is described in the end of editorial for this problem

  • »
    »
    8 years ago, # ^ |
    Rev. 2   Vote: I like it +6 Vote: I do not like it

    For the linear time solution approach try considering the following example:

    Suppose given input of : 1 4 2 3

    Then number of 0's=2

    And number of 1's=3

    Clearly 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 end

    01110 {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 ans

    01101 {1,4,2,3}

    • »
      »
      »
      8 years ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      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--;
          }
      }
      
»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I am lost in the prove of Div1C... Could someone explain the prove a bit less formal?

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +31 Vote: I do not like it

    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

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Great solution!

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

      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?

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

        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.

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

      Thanks for the help, now I get how it works. =)

    • »
      »
      »
      8 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      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!

    • »
      »
      »
      8 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      can u explain case when bestidx == -2 comes into play in your solution ? ie when sub[i] == best !

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

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.

»
8 years ago, # |
Rev. 2   Vote: I like it +25 Vote: I do not like it

can someone please explain how to solve div 1 D ? or is there a general method to solve this kind of flows ?

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

    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)

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

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.

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

    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

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

      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

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

        Then you go to the right first and to the left then

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

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).

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

    Thank you, it will be fixed as long as codeforces manage to take the new version of editorial from polygon.

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

In the main post you mentioned cheaters. What exactly happened?

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +12 Vote: I do not like it

    It happens from time to time, some guys send equal solutions for the problems and after the contest they are banned.

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

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.

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

Can anyone see this code for the strings question and tell me what's wrong with it? http://ideone.com/sFgcXB

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

    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".

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

      I did that and now its giving TLE on test 27. Could you tell me what modification needs to be done?

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

        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

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

        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.net/contest/709/submission/20125058

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

That may be a very stupid challenge, but it is also possible to solve Div1E for constraints like m ≤ 10, n ≤ 109 :P

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

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.

»
8 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hi. What is cnt array in Div1C (Centroids)? I have never heard this shortcut.

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

For div1 E,what's the a/b-the probability mean? Isn't it the probability that each block not been destroyed?

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

Where is the editorial for problem D?

  • »
    »
    8 years ago, # ^ |
      Vote: I like it -8 Vote: I do not like it

    At the top of the editorial it says:

    Here is the part of the editorial, other problems will come a bit later
    
  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It is already here btw

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

      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.

»
8 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

ignore

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

In the editorial for C shouldn't the -1 in the inequality be +1?

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

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

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    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.

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

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?

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

    Yes. This splits you tree in several connected components.

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

Hi, I always get TLE on test 21... I passed the test point which n = 400000 but I can't pass the 399999 one lol

Is it random? I use bfs instead to avoid stackoverflow.

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

Why this tutorial is not attached as the contest materials in the contest page?