vovuh's blog

By vovuh, history, 6 years ago, In English

988A - Diverse Team

Tutorial
Solution (Vovuh)

988B - Substrings Sort

Tutorial
Solution (Vovuh)

988C - Equal Sums

Tutorial
Solution (Vovuh)

988D - Points and Powers of Two

Tutorial
Solution (Vovuh)

988E - Divisibility by 25

Tutorial
Solution (Vovuh)

988F - Rain and Umbrellas

Tutorial
Solution (Vovuh)
Solution (step_by_step)
  • Vote: I like it
  • +37
  • Vote: I do not like it

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

Can you explain a bit about what the Convex Hull Trick and Li Chao tree are ? Most Div 3 contestants like myself have little-to-no idea about what they are :)

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

    Also check the solution by step_by_step which uses Convex Hull Trick and Li Chao tree (I added it to the tutorial)

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

      Are you sure that it works in n*log^2(n)? In my opinion it's n*log(n). Why is it log^2?

      Does it also use Convex Hull in some way?

      In my opinion the tree can: 1. Given a and b, insert a new linear function. 2. Given x0, print the maximum value f(x0).

      and it is done in a*log(a)

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

        It depends on solution. Solution by step_by_step works in O(n*log^2(n)), but there are another one solution that works in O(n*log(n)) (of course there are many solutions which works in O(n*log^2(n)), O(n*log(n)) and with many other complexities). I was describe one of them and i do not deny that in this problem exists solution in O(n*log(n)).

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

          Thanks for reply. Could you tell me why it’s log(n)^2? Does update on the tree take log^2? I can’t see why.

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

            This segment tree does the following:
            1) splits the query segment into log(n) segments (like all segment trees do)
            2) From each of these log(n) segments it goes down until it reaches the leaf, so for each segment it spends log(n) operations (the height of tree)

            If n = 2^k and the query segment is [0, 2^k — 2] then the number of operations will be (log(n) — 1) + (log(n) — 2) + (log(n) — 3) + ... = O(log(n)^2)

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

              Thanks, I understand now. So all Li Chao trees work in log(n)^2?

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

                No, if the size of segment tree is power of two and you update all elements then it works in O(log(n))

                So you can use it when you need Convex Hull Trick and the query coordinates are not big. Time complexity will be log(a) per update/query, where a is the minimal power of two greater than maximal coordinate.

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

                  So if I make updates on whole tree, then updating works in log(n), because I divide it into one segment and it goes down one way to leaf. So log(n). And query costs log(n) (from root to leaf).

                  If I change updating to whole tree, then it will work in a*log(a).

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

                  Yes, it will be O(a*log(a))
                  While writing my solution I didn't notice it)

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

                  Can we use this when the query coordinates are negative but their absolute value<=10^5?

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

                  Yes, you can add 10^5 to all your coordinates and change linear functions:
                  k*x+b -> k*(x+10^5)+b-k*10^5

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

                  Thank you,just one more question suppose there are n linear functions and for a given query we consider only functions from l to r. So can we use lichao tree in this problem?

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

Hard contest for Div3, I think that it is Div2. Statistics of successful solutions in comparison with previous Div3 and Div2 confirms this

  | #486 (Div. 3) | #485 (Div. 2) | #484 (Div. 2) | #483 (Div. 2) | #482 (Div. 2) | #481 (Div. 3)
-------------------------------------------------------------------------------------------------
A |          3308 |          4140 |          2291 |          3879 |           3503|          2493 
B |          2460 |          2763 |          2175 |          2952 |            636|          2508
C |          1313 |          1963 |          1416 |           743 |           1362|          2139
D |           292 |           600 |           370 |           392 |            130|           777
E |            64 |           501 |            14 |             9 |              7|          1066
F |            18 |             8 |             3 |             - |              -|           824
G |             - |             - |             - |             - |              -|           261
  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you share the statistics ?

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

    Maybe it's not that this round was too hard but the previous ones were too easy?

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

      Almost all my school mates are complaining about the round.

      I think maybe it's a bit too hard for division 3?

      Easier than div.2 rounds, but definitely harder than what I thought div.3 round should be.

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

        There are no really hard problems, even the problems E and F are close to typical Div2C problems, but not harder than Div2C. If i will make Div3 rounds much easier, it will lead to easy way to get 1600+ by participating in Div3 rounds.

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

          All problems were doable and I liked them, but I think there is too little difference in difficulty form C to F

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

          I would agree with you that the difficulty of E and F were quite normal.

          However C and D might be of inappropriate difficulty.

          What's more, STLs like map, vector or set shouldn't appear so frequently since the beginners may not make their way to STL so fast.

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

            My solutions for C and D are almost don't use STL expect sort and standard binary search method. This is not too hard to implement your own sort or your own binary search to find the element in O(log n) instead of O(n).

            But I agree with you that C and D should be much easier that they were in this round. And in these problems must not be too difficult ideas or algorithms as fast sorting or binary search.

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
          Rev. 3   Vote: I like it -22 Vote: I do not like it

          Reading your comment vovuh : I wonder , why Div-3 was even created!

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

            Reading your comments Player.01: I wonder that you first ask to make the problems harder, and when I did it, you wonder that they are too hard.

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

              My intention was to remind the setters that the logic behind creating Div-3 , which is not surely pulling them to Div-2 comfortably , but to set problems that would help them to train logically & rigorously. I mean , it should work as a guideline , don't you think? I believe , setters must be flexible to any feedback & take only the logical ones into considerations rather blindly defending his rounds. Thnx for reading my comments. Have a nice day!

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

                Please don't think that I'm blindly defend my rounds. I'm accept some good ideas from comments and draw my conclusions to do the rounds better than they are now. Have a nice day and sorry if I'm offended you.

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

    You also doesn't notice that there are different parts of community participate in div2 and in div3.

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

      From your post : " Probably, participants from the first division will not be at all interested by this problems. And for 1600-1899 the problems will be too easy."

      I am pretty sure Not all Div-2 participants solved All the "too esay" problems , even some Div-1 participants struggled with E & F .

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

        Maybe some div-1 participants didn't solve E & F. But that doesn't prove that they struggled with E & F. It may also happen that they found E & F too easy to try. I personally found D to be tougher than E & F. F was very basic DP problem. E was also simple, just handling corner cases were difficult. I haven't learnt anything by solving E & F. But I have learnt from D.

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

Can anyone please explain the use of npos in solution B — "if (s[i + 1].find(s[i]) == string::npos) " ?

Cplusplus.com says that it is the greatest possible value for an element of size_t , What does it means ?

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

    It is almost the same as -1 (means "not found" or some similar). I use it just because of my habit. You can replace it with -1 and nothing will change.

    I can be wrong and maybe someone else will explain it better than me.

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

    Just complementing Vovuh's answer: since npos is unsigned, -1 actually corresponds to the largest possible integer value. Since it's greater than the size of any string, it never equals a valid position, and is thus returned to indicate a failed search.

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

In solve 988D:

			if (isl && int(res.size()) < 2) {
				res = { lx, x[i] };
			}
			if (isl && int(res.size()) < 2) {
				res = { x[i], rx };
			}

Maybe it would be more correct?:

                        if (isl && int(res.size()) < 2) {
				res = { lx, x[i] };
			}
			if (**isr** && int(res.size()) < 2) {
				res = { x[i], rx };
			}
  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Woooops, yes, you are right! But surely this solution is also correct and the second if statement is useless :D It is because of you will check this pair of points when x[i] will be the right point and lx will be the left point.

    I will fix it anyway, thank you :)

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

      Nothing, but you again made mistake....:

                              if (isr && int(res.size()) < 2) {
      				res = { lx, x[i] };
      			}
      			if (isl && int(res.size()) < 2) {
      				res = { x[i], rx };
      			}
      

      You need to swap first line and 4th....

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

        Oh, I made the second mistake only in the Russian version of editorial, in English version all is fine :D Thank you, fixed again (I hope :D)

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

There is any other way to think/solve D?

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

vovuh,In problem C why did you use stable_sort? Normal sort also passed. What is the difference? In the future at what kind of situations should we use stable sort?

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

vovuh in F we can use another dp with O(a) memory and O(a2) time:

in case we have umbrella in i-th position: dp[i] = min i <= j (dp[j] + (j - i) * w[i])
if we don't: dp[i] = min(dp[i + 1], dp[i])

and when doing convex hull trick on this dp, we can see that slopes are decreasing, so we don't need additional structures to maintain convex hull. so this can be solved in O(a * log(a)) time

link: 38904415

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

Problem F — Someone can help me?, i can't find mi error :(.

My code

Thanks in advance c:

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

    If you have more than one umbrella at the same place, you should get the one with minimum weight

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

      You're right. Oh my god, i can't believe that i didn't consider it. Thank you. :D

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

      @Emiso i was having the same mistake. thanks a lot

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

In problem D tutorial, how is k equal to L ?

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

    If we transform to the binary representation, any power of two is represented by 1 and number of 0's (may be zero) to the right of that 1, for example: 1 and 1000, which are equal in decimal to: 1 and 8, respectively.

    So, when you sum two different powers of two together, you will get a number which is represented in binary as two 1's and number of 0's (may be zero), for example:

    10000

    +

    00100

    =====

    10100

    So, the result is not a power of two.

    But there is one case that will give us a power of two from summing two powers of two, and it is summing a power of two to itself, like:

    01000

    +

    01000

    =====

    10000

    Hope it answered.

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

      Thanks for the answer, it cleared a lot. But why does the answer not exceed 3? it could have been dist(a, d) * 4 instead of 3.

      upd: ok, I got it, in case of abcde there's still a abcd triplet in there, so the distance between a and c will still be 3*2^k, and it will be still invalid.

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

        Welcome :).

        The equation dist(a, d) = dist(a, b) * 3 came from the proofs before it.

        In the same way in which the tutorial proved that dist(a, b) = dist(b, c), we can prove that dist(b, c) = dist(c, d). So, the following equation is hold:

        dist(a, b) = dist(b, c) = dist(c, d) ( = x, for example).

        And we know that: dist(a, d) = dist(a, b) + dist(b, c) + dist(c, d) = x + x + x = 3 * x. And from here, 3 came.

        And any changing in this 3 means that some changes in the previous parts of the proof were happened, for example:

        dist(a, d) = 4 * x can come from: dist(c, d) = 2 * x, which means that: dist(b, d) = dist(b, c) + dist(c, d) = x + 2 * x = 3 * x, which isn't a power of two, but in the problem, we need dist(b, d) to be equal to a power of two.

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

          Can you maybe also answer how the term of 10^9 came in the equation when calculating the complexity?

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

            Sorry, I cannot reply now. I'll try later (may be toworrow).

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

            You iterate over all powers of 2, from 20 until 2x with 2x ≤ 2 * 109.

            There are many of these powers.

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

              Thank you, it would have taken with me much more words to answer it :)

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

Will the vector in problem C's solution too large? Cuz it maybe 2 * 1e5 * 2 * 1e5 elements...?

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

Why am i getting TLE on div 3 D when using map, and AC when using original binary search ?

Using binary search: https://codeforces.com/contest/988/submission/38916484

Using STL Map : https://codeforces.com/contest/988/submission/38916424

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

" You can also notice that the order of swaps doesn't matter and you can rearrange them in such a way that no leading zero appears on any step. " How to prove this or any kind of intuition behind this ?

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

    You can rearrange the operations in such a way that first of all you move the leftmost non-zero digit in the number to the left in order to keep the number correct after each of the next operation.

    But in general case you don't know which digit will be leftmost non-zero (there are too many cases that are depend on the digits you choose), so to make this problem much simpler you firstly move digits you choose to the right and then find the leftmost non-zero digit and move it to the left.

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

can anyone explain the 3rd case of 988F .

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

    Test Case:

    10 1 1
    0 9
    1 5
    

    the segment 0-9 is under rain and only haft an umbrella on the position 1 so you never can pass the position 0 without getting wet, so the output is -1

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

Can anyone tell, why http://codeforces.com/contest/988/submission/38851322 this solution didn't work for Problem C?

The approach given in the Editorial also fails in Python.

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

Why does the memory blow up on 7th test case? 38888128 (Problem B)

I know the complexity is O(n2)...but why does memory blow up?

Is it something to do with sorting string array?

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

Probably a simple solution to E : http://codeforces.com/contest/988/submission/38965567 As we have only 4 options 00, 25, 50, 75 at the end Just count the place by which these digit is to be moved obviously taking care of leading zeroes at the beginning in case the swap is made.

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

Can someone explain the problem with this approach for "Rain and umbrellas"?

let - dp[x][0] --> indicates that umbrella was not used for segment (x-1) to (x)

  • dp[x][1] --> indicates that umbrella was used for segment (x-1) to (x)

Now, dp[x][0]
= infinity (or invalid) --> segment (x-1) to x is under rain

= min( dp[x-1][0], dp[x-1][1] ) --> segment (x-1) to x not under rain

dp[x][1]
= (best wt umbrella uptil now) + dp[x-1][1] --> I continue carrying same umbrella

= (best wt umbrella uptil now) + min( dp[x-1][0], dp[x-1][1] )
--> I drop heavy wt umbrella and pick more lighter umbrella

Getting wrong answer. Not able to find whats wrong with the approach.

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

    The problem in your approach is best umbrella to be carried till position i may not be necessarily the best one for i+1 but other umbrella might help. For computing the optimal solution, you MUST consider all previous choices of umbrellas. This is the dynamic programming approach, what you BEST is actually greedy. Hope it helps.

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

Can anybody help me with this: http://codeforces.com/contest/988/submission/38960067 I can't find my mistake. Is stuck in testcase 5.

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

In problem F I use:

int dp[i] — the minimmum fatigue to get to position i,

bool rain[i] — true if position i is into a raining interval,

umbrellas[i] — the minimmum weight of an umbrella at position i, if there is no umbrella, umbrellas[i] = 0;

In my approach I calculate for each position i, what's the minimmum fatigue of getting there using the umbrella at position j (j <= i):

dp[i] = min( umbrellas[j] * (i — j) + dp[j — 1]) for each j from i to 0;

I can't see where my idea fails, still stuck in testcase 5.

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

    Not reading carefully the problem caused me multiple WA5, specifically this lines:

    he must carry at least one umbrella while he moves from x to x+1 if a segment [x,x+1] is in the rain (i.e. if there exists some i such that li≤x and x+1≤ri).

    Hope you don't come across the same mistake

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

about Problem D: why the size of the answer set can be 1?

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

Can anyone please tell me how standard compare function works or refer me any article about it?

»
22 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I'm tryna do F using the following approach but I'm getting WA on test 19 :

let the states of dp be the umbrellas. Now dp[i] keeps the minimal fatigue to reach the umbrella i. Now for all umbrellas I've kept the minimal fatigue to reach it. So now say the best or minimal fatigue to reach the destination, when the last umbrella that is used it say k, is dp[k]+(des-k)*weight[k]. Note that the destination is the last rainy segment's end point.

So I'll take the minimum out of all (reaching the destination with K as the last umbrella.)

This looks quite right intuitively but I'm getting WA on test 19. I've been tryna figure it out but couldn't.

Please help.

submission:
https://codeforces.com/contest/988/submission/161450389