awoo's blog

By awoo, history, 4 years ago, translation, In English

1437A - Marketing Scheme

Idea: adedalic

Tutorial
Solution (adedalic)

1437B - Reverse Binary Strings

Idea: adedalic

Tutorial
Solution (adedalic)

1437C - Chef Monocarp

Idea: BledDest

Tutorial
Solution 1 (pikmike)
Solution 2 (pikmike)

1437D - Minimal Height Tree

Idea: adedalic

Tutorial
Solution (adedalic)

1437E - Make It Increasing

Idea: Neon

Tutorial
Solution (Ne0n25)

1437F - Emotional Fishermen

Idea: BledDest

Tutorial
Solution (BledDest)

1437G - Death DBMS

Idea: BledDest

Tutorial
Solution 1 (pikmike)
Solution 2 (pikmike)
  • Vote: I like it
  • +88
  • Vote: I do not like it

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

Well i'm not so sure about this but i think another more straight forward answer for b is that we use the fact below

Spoiler

Once again I say as i'm still a beginner i'm not fully confident of this approach tell me if it has a problem

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

    I did this, but I don't have proof why this works.

    I saw that when there is a even length contiguous block of bad indices then one reversal fixes that block. But for odd length blocks it still works, I don't why, I was seeing that probably the odd length blocks cam be paired up with other odd blocks but couldn't conclude much.

    Anybody can prove this?

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

      idc123 You are almost correct. The reason this works is that since the number of indices that have to be reversed is always even (pretty straightforward), the number of odd blocks will be even in number. Two consecutive odd blocks can be made correct in 2 moves, the first move- reverse the whole substring between the first odd block tot he next odd block(both inclusive), then in the second move reverse only the middle part which was initially correct before the first move.

      So overall, 2 odd blocks require 2 moves, and they are even in number, so for the sake of counting we can say that we need 1 move per odd block, and 1 move per even block is obvious. So the answer is the total number of blocks.

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

        Ah, I see, I was unable to see why number of odd blocks is even, turned out it's because total bad indices are even. I think I had hard time realising this. Thank you for your comment!

      • »
        »
        »
        »
        4 months ago, # ^ |
        Rev. 5   Vote: I like it 0 Vote: I do not like it

        I don't think it is always true that two consecutive odd-blocks can be made correct in 2 moves.

        Consider s = 11011011100000. Let the bad index tracking string be p. Then, p = 01110001001010

        Now if you do reverse(s[2..8]) (1-based), it leaves you with s = 11101101100000 with corresponding p = 01000111001010. Then doing reverse(s[3..5]), it leaves you with s = 11101101100000 and p = 01000111001010. But at this step we should have corrected the adjacent bad odd segments and have had s = 10101010100000.

        Not only we don't have this but in this s, if it were correct, we would have basically generated new 0's which should not be possible.

        I think the issue is since the 4 zeroes are at the end in s, it is not possible to correct the first two odd segments without including the 0's. Maybe instead it is correct to say that for every odd length segment, we can find another odd length segment such that the they can be fixed in the way you described leaving everything else unchanged.

        Please correct if I made a mistake.

        Edit: In this case if we swap segments s[2...13], s[3...10], then it reduces the 4-odd segments to 2. We can then fix the whole string by swapping s[6...9] and s[7...8]

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

    Yup, you are right. I used this approach only. :-)

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

For G, can someone please explain why iterating over all the terminals in each query is $$$O(Q*sqrtN)$$$?

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

    Not exactly $$$q \sqrt n$$$ but $$$(sum~of~lengths~of~q) \cdot \sqrt{sum~of~lengths~of~s_i}$$$. No more than $$$\sqrt{sum~of~lengths~of~s_i}$$$ words can end in each position and there are $$$(sum~of~lengths~of~q)$$$ positions in total.

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

      why n^(1/2)? I think it should be n^(2/3).

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

        Why? If we want to find out the maximum number of unique lengths then let's take the smallest possible lengths $$$1, 2, \dots, k$$$, one string per each length. The sum of them is $$$\frac{k(k+1)}{2}$$$, thus $$$k$$$ is $$$\sqrt{2 \cdot total\_length}$$$.

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

          Let's conside the worst case, when you query for a string T, let's says T = abcd...z, and insert every substrings into the Automaton, There are O(|T|^2) substrings, and the total length of them is n = 1*|T| + 2*(|T|-1) + .... + |T|*1 = O(|T|^3).

          By subsititution, we have O(n^(2/3)) in the worst case. Did I make some mistake?

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

            I think you are mistaking what are we summing up. What we are doing with the query is the following: add the first letter, save the current vertex and look at the terminals above the vertex (their count is $$$O(\sqrt{dictionary\_length})$$$). Then go over the next letter from the saved vertex, once again jump no more than sqrt times. I don't really see how the substring count of the query is relevant here.

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

    for each search you can also just keep a set of which words you have already matched in the current search (actually a set with each terminal of the trie that has already matched). thus, if you are visiting it again during the same search, you just ignore it. then the first solution for G becomes O((q+T) log n), T being the sum of lengths of each query of type 2.

    97259249

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

      Why do you think the complexity is like that? I don't quite see how to construct a test such that there are a lot of distinct substrings of fixed total length but I don't see $$$O((q + T) \log n)$$$ either.

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

In E, I am not able to get why are we subtracting i from each a[i] int the following step forn(i, n + 2) a[i] -= i; in editorial solution.

Can somebody help me? Thanks in advance!

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

    Same Here. I didn't get that part For eg. if the seq is [2,5,3,1] then we can't choose LIS as [2,3] but by taking distance consideration and by making a[i]-=i how we are able to do that.

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

    If you subtract i, it leaves room for you to make the other elements in strictly increasing order.

    For example, if we have the array

    1, 1, 2, 3

    And we subtract i from each,

    1, 0, 0, 0

    You can see if we make these elements all equal, then the original array will be increasing. If we change everything to one and then add i again, we have

    1, 1, 1, 1 -> 1, 2, 3, 4

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

Hi which programming language is this: (answer to A) ? (I'm asking cuz it looks sexy)


fun main() { repeat(readLine()!!.toInt()) { val (l, r) = readLine()!!.split(' ').map { it.toInt() } println(if (2 * l > r) "YES" else "NO"); } }
»
4 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

adedalic "The longer the segment [a2,a) is the better and the maximum we can take is a=2l."
Can you please explain this statement a bit more? maximum a possible is 2*l but there can be a such that a<l and satisfies the constraints!! and we have to analyze that also.. Am I missing something?

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

    It has been shown that $$$\left\lfloor\frac{l}{a}\right\rfloor = \left\lfloor\frac{r}{a}\right\rfloor$$$. And also that, we must find $$$a$$$ such that $$$\frac{a}{2} \leq l$$$ $$$mod$$$ $$$a \leq r$$$ $$$mod$$$ $$$a < a$$$. So, it must be obvious that $$$r - l < \frac{a}{2}$$$, if such an $$$a$$$ exists
    Case I: $$$r \geq 2l$$$
    Here we must have $$$l \leq r - l < \frac{a}{2}$$$. That is $$$a > 2l$$$. But this wont work as $$$l$$$ $$$mod$$$ $$$a$$$ would be less than $$$\frac{a}{2}$$$
    Case II: $$$r < 2l$$$
    We can always choose $$$a = 2l$$$

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

      I got what you said, it becomes easy as soon as you have the two cases(r>=2*l and other one) to analyze.

      but what if we want to analyze cases as below
      0) obviously a should not belong to [l,r]
      1) if it is possible to have required a such that a>r, this gave me 2*l>r
      2) then a<l, this I couldn't get to any conclusion

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

        According to 0), either a > r or a < l. if a > r, obviously that a == r+1 is the best. Now we want to prove if a == r+1 is not satisfied, a < l will be not satisfied too.

        If a == r+1 is not satisfied, it means l*2 < r+1, right? Because Only in the situation that l mod (r+1) = l < (r+1)/2, the restriction is not satisafied.

        Next, l*2 < r+1 means l*2 <= r. If a < l, you will easily become aware of that for every a, exist a k that satisfy l <= k*a <= r. Because l*2 <= r.

        If l <= k*a <= r, the restriction is not satisfied. So we finally find if a == r+1 is not satisfied, a < l will be not satisfied too.

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

For Problem F, I've got a $$$O(n log n log max a_i)$$$ solution. Code

Basically $$$dp[i][j]$$$ means the number of ways to make fisherman $$$[1,i]$$$ emotional while $$$j$$$ of them are happy, considering the sequence in descending order.

Then use a segment tree with range multiply update and range sum query to speed up the solution.

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

G can also be solved with suffix tree. I dont know about Aho Corasick , but i heard that algorithm use in Aho-Corasick is similar .

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

It may be completely irrelevant but I just want to ask one doubt. Should I upsolve the problem which are rated at something greater than 2600 (For example F,G in this contest) or should I upsolve only problem which I currently feel comfortable with? (From experience, I can say I am able to understand 2400 rated problem if given some hours)

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

    If you just want to be high expert or low cm, you should probably just do easier tasks(<=1900). Though in theory, doing hard problems is a lot better but doing too hard tasks can make you demotivated, make you look at editorial too quick etc.

    I would suggest upsolve upto 200-400 rating points above you, depending on how hard they feel to you.

    Contests are about struggling with problems so also make sure you struggle before reading editorial.

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

why would greedy fail for C?

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

    what greedy?

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

      Like we use T for every distinct ti equal to it so answer will be 0 till then and for every duplicate ti we will use the smallest absolute difference to it's left(greater than 0) or right which has not been used and add it to the answer

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

        Any reason why this should work?

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

          we are trying to minimize the answer but now that it's known that greedy doesn't work, I just want to know why

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

            How will you choose which side you should take

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

              I will sort the initial array first and then if both sides have equal absolute difference then I will choose the left side as the right side may come in handy for further values of the array

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

                Just take this example: 1 3 4 4 6 7 7 8 9 10 11 12

                According to your logic, you'd take 5 for the repeated 4 because 2 is farther, however, taking 2 would be more beneficial because then you'd have 5 available for the repeated 7 later. I hope it's clear.

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

                  Thank you. This is the test case I was missing.

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

            And even your algorithm will not follow this: " I claim that there is an optimal answer such that the times T for each dish go in the increasing order. "

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

    Even I was doing same during contest but cant find why it is wrong https://mirror.codeforces.com/contest/1437/submission/96912740 .Help if anyone got it?

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

Can anyone please explain the implementation of problem C first approach. I understood the tutorial, but didn't understand implementation.
why do we use dp[i][j + 1] = min(dp[i][j + 1], dp[i][j]); ?

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

    Let's see what we are doing while iterating the for loop in the first approach-

    We are updating two values dp[i+1][j+1] and dp[i][j+1].

    We are updating dp[i+1][j+1] when it has larger value than dp[i][j] + abs(t[i] — j), which is in fact always (so you can try changing min(dp[i + 1][j + 1], dp[i][j] + abs(t[i] - j) to dp[i][j] + abs(t[i] - j)).

    Now to the real question- We are updating dp[i][j+1] when it has larger value than dp[i][j], as dp[i][j+1] currently stores dp[i-1][j] + abs(t[i-1] - j)(accept for i=0), if its more than dp[i][j] we update it.

    In simpler terms, we are just comparing dp[i][j] + abs(t[i] - j) and dp[i+1][j] and assigning the minimum of the two to dp[i+1][j+1].

    Try making dp matrix and dry run the code against some sample test cases, you will surely get it. Correct me if I'm wrong :P

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

    Here is an easy to understand approach :- Let dp[i][T] denote the minimum cost of processing the first i dishes (Remember we first need to sort them in the increasing order first), such that the $$$i^{th}$$$ dish is processed at time T.

    We can simply calculate answer for each state in O(n) hence the total time complexity will be O(n^3)

    Initial code

    Note that we can move the abs(j - arr[i]) part out of the min() function as it is not dependent on k, hence our code now becomes :-

    code

    So finally we can optimise it to O(n^2) by observing that the innermost loop only calculates the prefix minimum of dp[i-1] (even O(n^3) easily passes the time limit though).

    final code
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In the Hungarian implementation of Problem C, can anyone provide a general code that also includes finding which employee was allotted which job ? ( or in this case what was the final T for all input values) Thanks.

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

for B i used so simple approach and i have no proof of it but somehow it worked . if someone knows please tell me too. just counted max(adjacent count of(0) , adjacent count of (1)) = answer... thats it.. May be we need to do work when something is consecutive , this what i thought while solving

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

    I think in the editorial it is proved. Actually adjacent count of(0)==adjacent count of (1) if you link the a[0] with a[n-1]. So the answer is sum(s[i] == s[(i+1)%n]) / 2

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

Can anyone explain the second proposal in B's editorial which says there will be equal pair of 0's and 1's if we add 1 to the left and 0 to the right

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

    Either the no. of consecutive 0 pair and 1 pair are equal already or in the other case if no. of 1 pair is more than it could be only one more than the no. of 0 pairs and both ends will have 0 same for more 0 pairs than 1 pairs (I am getting this intuition) . I think this can be proved by using the fact that no. of 0s and 1s are equal, but didn't tried to prove yet. Sorry for bad english.

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

There's a simpler dp for problem F that doesn't require any optimization. The state is just $$$dp[i]$$$, which is just the number of ways in which we can choose fisherman $$$i$$$ to be happy (again, we have the fishermen sorted in non-decreasing order). The overall running time is $$$O(n^2)$$$.

My submission.

If you've got any doubts about what's going on here, feel free to ask :)

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

    I don't understand anything in there, can you explain it?

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

      Yeah, my bad. I should've realized that code is hard to read. I'll try my best to explain it.

      Firstly, the fishermen are sorted in non-decreasing order.

      Secondly, $$$smaller[i]$$$ is the number of $$$j < i$$$ such that $$$2 * a[j] > a[i]$$$. These are fisherman that we cannot place to the left of $$$i$$$ should we choose $$$i$$$ to be a happy fisherman. When I say some $$$j$$$ belongs to $$$smaller[i]$$$, it just means that $$$j < i$$$ and $$$2 * a[j] > a[i]$$$.

      Thirdly, when we have reached any state $$$i$$$, we have already placed all elements $$$j$$$ such that $$$j \le i$$$ excluding the elements which belong to $$$smaller[i]$$$, because as stated before, these are elements that we cannot place in front of $$$i$$$, so we must decide their positions later on after choosing some other happy fisherman (after changing maximums i.e. transitioning to another state).

      From here, I will describe the transitions in the dp ($$$dp[i]$$$ is the number of ways in which we can make fisherman $$$i$$$ happy). $$$i$$$ will denote the current state we are in, and $$$j$$$ the state we want to transition to. ($$$i < j$$$).

      When we make this transition, we place element $$$j$$$ in the first unoccupied position, and so there is only one way to place element $$$j$$$. The reason the first unoccupied position is always given to $$$j$$$ is because all the other elements that we are yet to be placed can only come after $$$j$$$, which in turn is because we cannot place it directly to the right of $$$i$$$ (either because they belong to $$$smaller[i]$$$ in which case we will get a content fisherman, or because they are just larger than $$$i$$$ and would result in a change in maximum).

      The other elements that we can place (and have to place) when making a transition from $$$i$$$ to $$$j$$$ are those belonging to $$$smaller[i]$$$ and those strictly between $$$i$$$ and $$$j$$$ but not belonging to $$$smaller[j]$$$. Let the number of such elements be $$$count$$$. Now we have some number of free positions that hasn't been occupied yet. This number is exactly $$$n - i + smaller[i] - 1$$$ (because we have already placed all elements before $$$i$$$ except for those belonging to $$$smaller[i]$$$, and have also placed $$$j$$$).

      We can freely place all $$$count$$$ elements in any order we wish in these positions. The variable $$$factor$$$ basically gives the number of ways to do this. We pick exactly $$$count$$$ of the free positions, and then we can freely permute the order of the fishermen in those positions, thereby giving what you see in the code.

      Then we just sum over all valid $$$j$$$ (i.e for all $$$j$$$ such that $$$a[j] \ge 2 * a[i]$$$) for each $$$dp[i]$$$ thereby giving an $$$O(n^2)$$$ solution. The answer is just $$$dp[0]$$$ (according to how I've implemented it), and by default I take $$$a[0] = 0$$$.

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

Problem F is solvable in nlogn. Here is the code link https://mirror.codeforces.com/contest/1437/submission/96998991.

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

I can not understand the editorial of problem A,someone please explain in more detailed way.

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

    For a continuous interval modulo operation, the result is continuous.

    So just let a = r + 1 and check if l >= a / 2

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

check out for the simple approach for B submission

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

    Can you please explain the idea behind that code? Thanks in advance.

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

In D, what we exactly needed to do? My approach:-

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

    We will not increase depth every time when v[i]<v[i-1]. We have to find the minimum depth.

    So if there are more nodes in the above level, we can just switch to another node (the node which is a sibling of the parent node).

    We increase the depth only when there is no node left with zero child in the above level.

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

Problem A was rated 800. Is it a joke? I know it's a one liner, but just look at the number of people solved it during the contest. This problem is at least 1000. Probably 1100. People got angry because of the div. 4 rating inflation and now they increased the difficulty of all div 2 contests leaving the problem rating as before. Amazing job, guys.

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

Can someone explain the "slope trick" mentioned in the editorial of problem c. I do get the o(n^2) solution. The o(nlogn) solution seems to be interesting

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

Problem B was too tricky for div2 B..

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

For Problem C, i used greedy approach by looking at the tag which is on the problem statement. But greedy actually don't work. In that case, are people above 1900 misleading with tags or is there actually a greedy solution for problem C ? Please let me know. Thanks.

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

    Ig sorting by time taken for each dish is greedy part

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

The B solution is so precise.I learn a lot from it!

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

G let me review the AC automaton... but TLE on test 70 :(, maybe cuz the pointers in my ugly code spend too much time...

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

    Passed! I used a heuristic method: maintain a pointer "nex", which is the closest node in the fail tree that is the end of some $$$s_i$$$. When initing the fail pointer of AC, the nex pointer can be inited by the way. Then, we can jump faster. I don't know whether the worst complexity is still $$$O(q\sqrt n)$$$, but it works.

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

For problem G, why is "Thus, that's bounded by the square root of the total length." true? (Actually, I'm also confused by what's meant by "that").

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

Problem B. Can someone explain why is the answer number of consecutive pairs divided by 2?