LMydd0225's blog

By LMydd0225, 8 months ago, In English

2146A - Equal Occurrences

Hint 1
Hint 2
Tutorial
Implementation

2146B - Merging the Sets

Hint 1
Hint 2
Tutorial
Implementation

2146C - Wrong Binary Search

Hint 1
Hint 2
Tutorial
Implementation

2146D1 - Max Sum OR (Easy Version)

Hint 1
Hint 2
Hint 3
Tutorial
Implementation

2146D2 - Max Sum OR (Hard Version)

Hint 1
Hint 2
Hint 3
Tutorial
Implementation
Another solution using greedy on trie
Implementation

2146E - Yet Another MEX Problem

Hint 1
Hint 2
Hint 3
Tutorial
Implementation
Bonus

2146F - Bubble Sort

Hint 1
Hint 2
Hint 3
Tutorial
Implementation
  • Vote: I like it
  • +201
  • Vote: I do not like it

»
8 months ago, hide # |
 
Vote: I like it +39 Vote: I do not like it

We should appreciate fast editorials :))

»
8 months ago, hide # |
 
Vote: I like it +17 Vote: I do not like it

Thanks for the fast editorial! (Maybe the fastest)

»
8 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

Thanks for fast editorial

»
8 months ago, hide # |
Rev. 2  
Vote: I like it +10 Vote: I do not like it

FAST EDITORIAL!! Thanks for the contest :)

»
8 months ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

Appreciations for the fast editorial!

P.S: E was so good ( submitting it just 3 seconds before made the adrenaline rush worth it :D) Loved the contest!

»
8 months ago, hide # |
Rev. 2  
Vote: I like it +1 Vote: I do not like it

insanely fast editorial ty

couldn't do B because I wasn't convinced iterating over sets individually was fastest D:

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

    Yes such problems always require reading the constraints thoroughly before solving. This has happened with me before. I learnt my lesson last time

»
8 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

How to solve D with trie?

»
8 months ago, hide # |
Rev. 2  
Vote: I like it -9 Vote: I do not like it

ultra fast editorial

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

WOW faster than nasa's internet speed!

»
8 months ago, hide # |
Rev. 4  
Vote: I like it 0 Vote: I do not like it

fast editorials!

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

Not able to submit code in first ~15 minutes.

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

    During this period of time, the judge system is still testing the solutions and using all (?) of their resources for it. Once all of the solutions have been rejudged, you will be able to submit again...

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

      I meant I cannot submit code during the first 15 minutes of the contest. The submission page is totally frozen and I cannot paste my code.

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

        really?? Wow, that's pretty strange. I guess for future cases like this you could use the mirror site/submit as file...

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

editorials with HINTS are sooo sooo good for practice !!

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

In C's editorial it is mentioned that we can reverse [l,r] but it length is odd won't it be incorrect because for middle elements p[x]=x and it will be stable then

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

    for every s[i] = '0' either there should be smaller element to its right or larger element to its left , if one of the condition hold true then it is fine , when p[x] =x in the case you mention then there are still elements to its right which are smaller then x , this is the reason why it works.

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

    that is only in the case when you guess m = x randomly. to be stable it needs to hold in all cases.

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

Can D be solved with bit Trie?

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

    Yes, i used trie for D1, but couldn't solve D2 :(

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

    I solved both with trie,I thought that's what everyone else would also have done but to my suprise I didn't found any another submission using trie in my friendlist. I used simple binary trie logic which we generally used to find max xor of an array with any element x,but instead of moving from msb to lsb Moved from lsb to msb so that Gap is not wasted,by Gap mean like in 100 and 001 if we take these two then or will be 101 hence there is 0 between ones which is Gap in my logic.

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

respect++ for fast editorial

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

Can someone explain problem E more clearly?

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

    Let ans[x] = answer if MEX = x.
    Move from left to right. At i'th step we have ans[] array calculated for all elements to the left of i. Now need to update array ans[] for current i:
    ans[a[i]] = 0 because since a[i] mandatory exists (in every subarray (l, i) ), MEX can not be = a[i];
    ans[x] for x < a[i] gets increased by 1 because if MEX x < a[i], then appending a[i] does not change MEX, but adds 1 to answer;
    ans[x] for x > a[i] remains the same, because appending a[i] does not change MEX, but doesn't change the answer.

    To implement it in O(N * logN) a powerful data structure is needed. I've used segment tree with lazy propagation, but may be there are other options.

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

Solution link using Trie Bit for D1 & D2

Idea
  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it +14 Vote: I do not like it

    How does it work? I tried to greedily search from $$$30$$$ to $$$0$$$ but failed. Could you explain why from low to high works?

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

      I'm not sure either, i also tried from 30 to 0 at first, but it didnt pass the samples so i just reversed it and it ACed (literally prove by AC)

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

        I have the same. Tried D1 from the greatest bit and failed on D2. But after i tried from the least bit and it works! Idk know why, lol.

        • »
          »
          »
          »
          »
          8 months ago, hide # ^ |
          Rev. 3  
          Vote: I like it +10 Vote: I do not like it

          The reason is that when we traverse from high to low, if a desired bit is not present, we switch paths to take an available number. However, this may lead us to a number with fewer bits. On the other hand, when traversing from low to high, the correct number is retrieved.

          P.S. — Example

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

            Can you explain why traversing from 0 to 30 find a number with more set bits compare to 30 to 0 because in both approach we are changing path as when we required it.

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

Thank you so much for really good problems and the fast editorial.

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

Didn't you wanted to say, in tutorial of problem E, that we want to increase b_i by 1 for all i < a_r ?

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

nice contest, speedforces

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

oh just compare my time limit submission with my accepted submission on D2

i can not sleep tonight

»
8 months ago, hide # |
Rev. 2  
Vote: I like it +3 Vote: I do not like it

How is checker for problem C made?

Edit:Understood

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

for problem tags on C, there are 8 binary search tags . Is that going to be fixed ?

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

the fastest round I have ever witnessed!!!

»
8 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it
  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it +13 Vote: I do not like it

    Why using heap. I did with trie only. 339778312

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

      Hi. Can you explain why it is optimal to solve it from the lowest bit to the highest bit?

      • »
        »
        »
        »
        8 months ago, hide # ^ |
        Rev. 2  
        Vote: I like it +4 Vote: I do not like it

        Here is a case where high to low bit will fail.

        l = 3, r = 6

        high to low trie -> 26

        low to high trie -> 28

        • »
          »
          »
          »
          »
          8 months ago, hide # ^ |
          Rev. 3  
          Vote: I like it +1 Vote: I do not like it

          Thanks for your hack, but I just can't understand why it is optimal from low bit to high bit. Could you make a explanation?

          If insert numbers into trie from high bit to low bit, and get the answer from $$$l$$$ to $$$r$$$, it will get WA on $$$[3, 6]$$$ where the correct answer is $$$28$$$, but $$$26$$$ is given.

          If insert numbers into trie from high bit to low bit, and get the answer from $$$r$$$ to $$$l$$$, it will get WA on $$$[1, 4]$$$ where the correct answer is $$$20$$$, but $$$18$$$ is given.

          I solved D1 with the method of traversing from high bit to low bit and get the answer from $$$r$$$ to $$$l$$$ but failed on D2. It's really confusing.

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

            OR is monotone but not lexicographic — adding a 1 in any bit helps, but there's no strict ordering where higher-bit wins over all lower bits combined. MSB-first greedily optimizes the current bit or breaks ties locally; but maximizing total number of 1 bits (or numeric value of OR) can require sacrificing an MSB tie to gain many 1s in lower bits. That requires deferring decisions about higher bits until you’ve examined lower bits → exactly what LSB-first does.

          • »
            »
            »
            »
            »
            »
            8 months ago, hide # ^ |
            Rev. 2  
            Vote: I like it +3 Vote: I do not like it

            Also in D1, all numbers $$$[0,r]$$$ are present. Therefore, all numbers' XOR are present, but in D2 as the exact XOR can be absent, can lead to picking up wrong number.

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

              But hey can you prove that going from LSB to MSB is always optimal, you showed a case where MSB to LSB fails. But can you prove there exists no case where LSB to MSB fails.

              suppose there exist a case where sacrificing a higher a bit is not optimal but we won't know about it because we already made the decision before reaching highest bit

              • »
                »
                »
                »
                »
                »
                »
                »
                8 months ago, hide # ^ |
                Rev. 3  
                Vote: I like it +5 Vote: I do not like it

                Sorry to disappoint you mate, I don't have a concrete proof for this. I did this question purely based on my intuitions which I wrote above, I solved D1 using different approach not trie-based.

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

                  Seems that,to some degree,there exists some equivalence. Consider the solution in the editorial,assuming that the current interval is [l,r] with the t defined in the solution. Without loss of generality,let length of [l,t)<[t,r].With the way in the solution,we pair each x of [l,t) with 2t-1-x. For certain x,if x+t<=r,assuming it finally match with y,then we can swap the matches.To be specific,in the solution it matches like (x,2t-1-x) and (x+t,y),however the result of (x,y) and (x+t,2t-1-x) is the same,because it holds that y&t=t.That's the stage of t,and for later stages with t'<t,it is obvious that the swap holds. To be vivid,assuming we have a values with t and b values without t,we only have to assure we get min(a,b)*t,but how we get it doesn't count. Thus,your solution is clearly right.On the other hand,if we start from the highest bit,for [l,r],if it could be described as [l,t),[t,2t-l-1],[2t-l,r] where r<t+l,then if we greedily try r,r-1,...,we get a wrong solution(where [2t-l,r] match with certian value in [l,t),get the t,however lose t'<t,and the swap mentioned above is no longer equivalent)

        • »
          »
          »
          »
          »
          7 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          I used high to low trie and it still passed.

          343100466

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

      Most of the people had same thought process. I recently solved a problem with BitTrie + remove() method. So, I tried with this and I think this is easier.

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

      Can you please explain your code?

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

Oh WHOA, that was fast.

I mean I checked in after 2-3 hours and it's already here, damn!

Great problems!

»
8 months ago, hide # |
Rev. 2  
Vote: I like it -10 Vote: I do not like it

Can Anybody help with why my code is giving the wrong answer? I was very convinced that it should work, but it's failing somewhere on test case 2 for Problem C — Rabbits, and I badly want to see where it's breaking.

What I am doing is basically seeing where the current rabbit is facing, if its face is to its left — In this case, I will need some right counter which means rabit facing left.

If the current rabbit can face either left or right, or it's not decided yet, I will mark undecided so that I can resolve it based on the next rabbit. If the next rabbit is undecided too, I'll pass the counter undecided until it's resolved.

Solution

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

Thanks for the tutorial. It’s really useful.

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

good contest

»
8 months ago, hide # |
 
Vote: I like it +2 Vote: I do not like it

I love problem C

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

In problem B

Original problem deals with n sets S1, S2, ..., Sn and asks whether there are at least three ways to choose some sets so that every integer from 1 to m appears in at least one chosen set.

Now, suppose instead of sets, we are given n arrays that may contain duplicate elements.

How should we approach this problem efficiently when arrays may have duplicates?

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

I'd solved A,B,D but with problem C, I'm still struggling as I was not able to understand it even now! What I got is if Si==1 then in Pi=i and for all other Pi!=i. 1 more case is if count of 1's is even then print NO. What is this problem

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

As for the trie solution to problem D, can anyone explain why it is optimal to solve it greedily from the lowest bit to the highest bit?

»
8 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

Cool round, thx!

»
8 months ago, hide # |
 
Vote: I like it +12 Vote: I do not like it

In the Editorial of problem C, in second point shouldn't l become m+1 instead of r = m-1

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

In the Editorial of D2,I find that the example of [11,16] is not the biggest one. It's less than [12,11,16,15,14,13].A small mistake.

»
8 months ago, hide # |
Rev. 3  
Vote: I like it +4 Vote: I do not like it

There is one more way to prove the solution for Problem D1 using induction and the following two observations:

1 → If we make a partition between a number x = (power of 2) and x - 1, then with respect to this partition, the pairs (x - 1 , x), (x - 2, x + 1), … will be complements of each other. For example: (7, 8), (6, 9), (5, 10) ............. wrt. to partition between (7, 8).

2 → If we take numbers 0, 1, 2, 3, 4, 5, …, r and let x = (maximum power of two such that x ≤ r), then the (count of numbers from x to r) will always be ≤ (count of numbers less than x). More Formally (r - x + 1) <= (x + 1). In short, there are always x + 1 elements from 0 to x — 1 in left, while on the right side (including x) there can be at most x elements. Otherwise, x would become the next power of two, which contradicts our assumption.

For example, if r = 15:

(Maximum power of 2) ≤ r ⇒ x = 8

Count of numbers from x to r = 15 - 8 + 1 = 8

Count of numbers from 0 to x — 1 = 8

Now, just make a partition between x = (max power of 2 ≤ r) and x — 1, do the pairing as in point 1. From point 2, the elements on the right side of the partition will always end first. So, after the first iteration of pairing, we again get a new unmatched array → (0, 1, 2, 3 ....... x).

Now You have the similar subproblem will lesser value of r.

for example if r = 10

first partition -> 7 | 8
pairs (7, 8), (6, 9), (5, 10)

remaninig array -> (0, 1, 2, 3, 4)
second partition -> 3 | 4
pairs (3, 4)


remaninig array -> (0, 1, 2)
third partition -> 1 | 2
pairs (1, 2)
»
8 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Problem E is nice . I tried — like extending ans[i] to ans[i+1] from left to right , right to left . consider all mexs . iterating from left to right . form segments of the bigger number increasing the answer ; nothing properly clicked like the stuff being done in editorial . Thanks .

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

can anyone tell why author's code for D1 is giving TLE for input t = 1 l = 4, r = 5

»
8 months ago, hide # |
Rev. 5  
Vote: I like it 0 Vote: I do not like it

Time complexity : $$$O(n\log n)$$$ 340097439

I come a solution different to author,the idea is complement.
First when i don't know how to do,then i try something greedily,let say $$$x$$$ is a number between $$$l=0$$$ and $$$r$$$ which is $$$l=0\le x \le r$$$,if i want to produce the optimal sulution i should let $$$x$$$ and ~$$$x$$$ combine to do bitwise or,~$$$x$$$ denoted 1's complement of $$$x$$$,why?The reason is according to the bitwise or defination at least one $$$1$$$ will leading the result become $$$1$$$,so in optimize way we should use $$$0$$$ to bitwise or with $$$1$$$,instead of $$$1$$$ bitwise or with $$$1$$$,until now i don't know is that possible to make $$$x$$$ can always combine with it complement or not then i try to simulation,i accidentally found that there is working.

Algorithm
Let $$$k$$$ be the number such that $$$2^k \le r \lt 2^{k+1}$$$.The algorithm is work as below,First let $$$x \in [2^k,r]$$$ and the $$$\text{mask}$$$ be $$$\underbrace{1111\dots 111}_{k+1}$$$ , and $$$\oplus$$$ denoted bitwise xor then $$$x$$$ and $$$x \oplus \text{mask}$$$ will combine together,then after this,let $$$y \in [l=0,2^{k}-1]$$$ and the $$$\text{mask}$$$ be $$$\underbrace{1111\dots 111}_{k}$$$ , if $$$y$$$ and $$$y\oplus mask$$$ haven't been combine then they combine together,otherwise continue. At the end,there may some remain element doesn't combine with other and the element are $$$[l=0,r'\lt r]$$$,every iteration will decrease the most significant bit of $$$r$$$ at least one bit,when $$$r == 2^m-1$$$ then directly $$$x\in [l=0,r]$$$,$$$x$$$ combine with $$$2^m-1-x$$$.

Proof
$$$\text{Why when }2^k \le r \lt 2^{k+1} \text{ then } x\in [2^k,r]\text{ always find a 1's complement?}$$$
$$$\text{Answer : since } 2^k \le r \lt 2^{k+1} \text{ then }0 \le r-2^k \lt 2\cdot 2^k - 2^k \text{ which is } 0 \le r-2^k \lt 2^k\text{ the element below }2^k\text{ is more than }r-2^k$$$

$$$\text{Why every iteration will decrease at least one most significant bit?}$$$
$$$\text{Answer : From the previous question,we already prove that every }x\in[2^k,r]\text{ will exactly find one 1's complement to combine,so after the remain element will decrease at least one most significant bit}$$$

»
7 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Now I also love problem E

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

Wrong binary search gave so many chills. Learnt a lot thank you!

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

Can someone explain how to solve D1? Using trees

  • »
    »
    3 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    trie ig

    (not my solution I read that in the comments section) so if you really want to know how they solved it you'd have to read the comments