-firefly-'s blog

By -firefly-, history, 9 months ago, In English

I hope you all enjoyed the contest!

Rating Predictions
Rate the contest!

2131A - Lever

Idea: -firefly-
Preparation: -firefly-
Analysis: temporary1

Hint
Solution
Code (Python)
Rate the problem!

2131B - Alternating Series

Idea: -firefly-
Preparation: -firefly-
Analysis: reirugan

Hint 1
Hint 2
Solution
Code (Python)
Rate the problem!

2131C - Make it Equal

Idea: Tobo
Preparation: Tobo, -firefly-, Friedrich
Analysis: Tobo

Hint
Solution
Code (Python)
Rate the problem!

2131D - Arboris Contractio

Idea: -firefly-
Preparation: -firefly-
Analysis: __baozii__

Hint 1
Hint 2
Solution
Code (Python)
Rate the problem!

2131E - Adjacent XOR

Idea: -firefly-
Preparation: -firefly-
Analysis: __baozii__

Solution
Code (Python)
Rate the problem!

2131F - Unjust Binary Life

Idea: efishel, __baozii__
Preparation: -firefly-
Analysis: temporary1, reirugan

Hint 1
Hint 2
Solution
Code (C++)
Rate the problem!

2131G - Wafu!

Idea: -firefly-
Preparation: -firefly-
Analysis: __baozii__

Hint 1
Hint 2
Hint 3
Solution
Code (Python)
Rate the problem!

2131H - Sea, You & copriMe

Idea: __baozii__, -firefly-
Preparation: -firefly-
Analysis: -firefly-

Hint 1
Step 1
Hint 2
Step 2
Code (C++)
Rate the problem!
  • Vote: I like it
  • +110
  • Vote: I do not like it

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

fast editorial :)

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

I'm really into reading editorial after bombing H with 12 submissions :)

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

fast editorial, nice :D

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

All the rate the problem sections are linked with each other. This shouldnt be the case right?

I.E. if i rated for E it also shows up on A,B,C,D,F,G,H

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

I like editorials that explain the solutions step by step, hint by hint. Thanks -firefly- for the great and fast editorial!!!

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

Cool H, love it. Sadly I don't have time to do other problems.

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

NICE EDITORIAL!

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

What's the problem with my solution to problem B?

333439457

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

Thanks for an Amazing Contest..

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

The input is 1 6 and the output is -1 3 -1 1 -1 1, why not

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

can anyone please provide code for the 2131D - Arboris Contractio??

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

Great problems and fast editorials, thanks!

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

Great problems!

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

I dont understand why my solution for D fails, I find the node that has maximum number of leaf nodes with depth 1 then I root the tree at this node, now for attaching all the other leaf nodes to this root node will take one operation per leaf, can someone tell me what's wrong with this approach?

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

    I did something similar (or maybe the same) that worked. I found the node that has the most leaves attached. Then the answer was the {total number of leaves} minus {most leaves on one node}.

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

    I did not see your code, but i have a suspicion that there is a difference between what you wrote in plain English vs what you wrote in code. Consider this tree:

    1 — 2 — 3 — 4 — 5

    Now, does your code treat $$$3$$$ as root? if so, it will find 2 leaves of $$$1$$$ and $$$5$$$ whereas $$$2$$$ or $$$4$$$ as root will only find leaves $$$5$$$ or $$$1$$$ respectively, resulting the answer to be 1.

    I implemented my code in a way so that i take the highest degree vertex as root, and find leaves that are not connected to it. This was "almost" correct, except the fact that the above mentioned case needs a tie-breaker to treat $$$2$$$ or $$$4$$$ as root instead of $$$3$$$ even though they all have same degree.

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

Problem H can also be solved if sum of M is not bounded by $$$10^6$$$.
Submission: 333367734

  • Let $$$f(d)$$$ be global array of size 1000000.
  • For calculating $$$f(d)$$$ can also be done in $$$O(2^7 * N)$$$ since f(square free numbers) is only needed.
  • After every testcase, we can clear only the relevant entries in the frequency table, which still yeilds same complexity
»
9 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Problems C and D are interesting, although E seems a little too easy for its place. Unfortunately, I lost too much time on B and couldn't solve F. But overall this was a nice contest!

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

I solved H using random. I shuffled the initial array and searched for the first coprime pair in the prefix, then the second one. 22 iterations were enough.

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

Nowcoder has a problem that is same to 2131H nearly:牛客练习赛137F,2131H just is the special case when g=1 in Nowcoder Practice 137F。

I just submit my code after changing g=1 and get AC.

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

I reduced H to having a bipartite graph of prime factors and indices, then there is $$$ gcd(a_i, a_j) = 1 $$$ iff $$$ dist(i, j) \gt 4 $$$ but I couldn't solve further. Can it be solved like this? here dist refers to minimum distance

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

Am I the only one who thinks problem F was slightly more difficult than problem G?

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

    yeah... I didn't solve G but pretty sure I already know how to do it without looking at editorial. But I got stuck doing F not knowing how to quickly sum all the costs/contributions. Should've done G instead.

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

    Definitely, I couldn't do F in contest, and now I did G with little trouble.

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

Hey everyone,

I've come across some people cheating again. I already wrote a blog about it last time and reported them in the known cheaters’ database, but unfortunately, they’re still getting away with it—and even showing off the ratings they earned dishonestly, especially around campus.

I'm honestly not sure what more can be done, but it's frustrating to see people benefiting from this while others are putting in real effort. Any suggestions on how to handle this in a more effective way?

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

If anyone struggling to find corner case for D, consider this tree:

1 — 2 — 3 — 4 — 5

Now, does your code treat $$$3$$$ as root? if so, it will find 2 leaves of $$$1$$$ and $$$5$$$ that are not connected to root whereas $$$2$$$ or $$$4$$$ as root will only find leaves $$$5$$$ or $$$1$$$ respectively, resulting the answer to be 1.

I implemented my code in a way so that i take the highest degree vertex as root, and find leaves that are not connected to it. This was "almost" correct, except the fact that the above mentioned case needs a tie-breaker to treat $$$2$$$ or $$$4$$$ as root instead of $$$3$$$ even though they all have same degree.

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

    To Get answer basically you have to take all leaf nodes and connect them to your root along with all that come in between so you can count how many leaf nodes current node is conncted to and treat that as degree and take maximum of that and leafs which are not connected to that root you selected with maximum leaf is your answer.

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

the explanation for E is easily explained!

thanks for a good div 3 round, enjoyed a lot

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

Personally, I found E to be bit easier than it should have been for it's position. It was an exciting contest. Kudos to the authors and organisers !

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

Great problems and fast editorials, thanks!

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

If anyone codes in Java please reply to this comment

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

In E, please tell me what wrong in this approach. For every element of the array if they are not equal i move towards right taking xor of elements on the way, till i reach a point where the cumulative xor becomes equal to the element i started with lets say index of that element is 'r'. then i move backwards from 'r' till the initial index. updating all the element in the process cumulatively, if in doing so i encounter any index a[i]!=b[i] i return "NO" else i repeat from 'r+1' index the same algorithm.

https://mirror.codeforces.com/contest/2131/submission/333453490 this is the code, please help!

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

    If I understand correctly, in the problem statement, it says the XOR operation can be performed at most once for each index.

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

      yes i know, if i frame my algorithm in other words, i start from index 'l' and 'r' doing xor of element from l to r gives a[i], then i say that all the element from l to r should be like, a[i]= a[i]^ a[i+1] ^ a[i+2].......^a[r] a[i+1]= a[i+1] ^ a[i+2] ^a[i+3] .... ^a[r] a[i+2]= a[i+2]^ a[i+3] ^ a[i+4] .....^ a[r]

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

In problem E why a[i] ^ b[i+1] should be equal to b[i] ? Can anyone explain

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

    b[i+1] essentially represents a[i+1] ^ ... ^ a[j], where i+1 <= j <= n.

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

    For a particular a[i], we can only XOR with a[i+1] that is fixed. Now a[i+1] can be as in original a array or it has already XORed itself with its neighbour(a[i+2]) such that it is wanting to be equal to b[i+1] after xoring it with its neighbour. So, for a[i] we have two choices , either XOR it with original a[i+1], or changed a[i+1] which is aimed to be equal to its own corresponding element, i.e., b[i+1].

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

I find problem B statement is very confusing to me. Also I'd rate E <= D.

Why does B in every contest choke me even harder than C :(

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

Nice contest, When I solved A in 3-4 mins I thought finally I'll go around pupil but than B humbled me by not getting one test case and C was just :), Tried solving in python TLE, Tried using optimized code TLE than converted the python code to CPP using tools and boom it worked :) I guess this time it's negative delta for me

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

Very nice E

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

For me C was harder than E lol. Don't know how alot of people solved it.

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

Can anyone explain me the solution of C?

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

My solution on C gave Time limit exceeded on PyPy3 but the same code worked on Python3. I think it's due to hash-collision problems.

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

E was pretty easy this time. When I thought of my logic, I was very sure that it would fail on the 2nd test case (I thought it cound not be this easy), but unexpectedly, it got accepted. I was very happy :)

I just traversed the array two times:-

1st time from starting, updating the values for every a[i], where a[i]^a[i+1] == b[i] and if a[i]==b[i] then continue.

2nd time from behind, again checking the same conditions, but with updated a[i] values.

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

This was a great contest, thanks for the authors.

I have some idea to discuss:

I don't really know how to implement the fft algorithm, but I have a speculation that problem F can be solved using that algorithm (which I was surprised that tags did not include) ...

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

    If I understood you correctly, d(i,j)'s formula is wrong. When doing an operation, table's row or column is inverted, while your formula works as if only 1 cell was impacted. For example, in a case a = "1", b = "000000", d(1,6) = 6, which isn't true.

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

      To be honest, I did not understand why would the algorithm fail when more than 1 cell is impacted by an operation, I would like to suggest an example so we can communicate better, consider the following input:

      1
      4
      0011
      0101
      

      We can conclude that the shape would look like this:

      0  0  1  1
      1  1  0  0
      0  0  1  1
      1  1  0  0
      

      The when cutting into areas and marking them with their depth (cost of reaching them if we start from $$$(0,0)$$$) we get the following:

      0  0  1  1
      1  1  2  2
      2  2  3  3
      3  3  4  4
      

      Or we can represent using arrays of heights and widths as:

      vector<int> height = { 1, 1, 1, 1 };
      vector<int> width = { 2, 2 };
      

      meaning the areas would be distributed in the following manner:

      depth | list of areas             | total area | total cost |
      0     | h[0] * w[0]               | 2          | 2 * 0 = 0  |
      1     | h[1] * w[0] + h[0] * w[1] | 4          | 4 * 1 = 4  |
      2     | h[2] * w[0] + h[1] * w[1] | 4          | 4 * 2 = 8  |
      3     | h[3] * w[0] + h[2] * w[1] | 4          | 4 * 3 = 12 | 
      4     | h[3] * w[1]               | 2          | 2 * 4 = 8  |
      total |                           | n^2 = 16   |         32 |
      

      meaning the final output should equal $$$32$$$ (based on my calculations).

      If you still see an issue with this approach I would be thankful to have an insight.

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

        Sum should be equal to 28 and the matrix d should look like this:

        0 0 1 2

        1 1 2 3

        1 1 2 3

        2 2 3 4

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

          I now realize what you were telling me about the single cell, thanks for pointing this out, the issue emerged because I mistakenly thought that the whole width/height segment (all rows that intersect the area) is affected by a single operations, and therefore there was an issue in area division, because the depth does not go up we the bit switches, but rather when you encounter a $$$1$$$ bit, and in this case, (as you kindly mentioned) the area partition would look like this:

          0  0  1  2
          1  1  2  3
          1  1  2  3
          2  2  3  4
          

          Meaning the calculations would be:

          vector<int> height = { 1, 2, 1 };
          vector<int> width = { 2, 1, 1 };
          
          depth | list of areas                           | total cost |
          0     | h[0] * w[0]                             | 2 * 0 = 0  |
          1     | h[1] * w[0] + h[0] * w[1]               | 5 * 1 = 5  |
          2     | h[2] * w[0] + h[1] * w[1] + h[0] * w[2] | 5 * 2 = 10 |
          3     |               h[2] * w[1] + h[1] * w[2] | 3 * 3 = 9  |
          4     |                             h[2] * w[2] | 1 * 4 = 4  |
          total |                                         |         28 |
          

          precisely what you told me, meaning the algorithm (after adjusting the definition/method of area partitioning) is supposed to work well for any test case (for as far as I see)

          In conclusion, do you think there is an fft algorithm optimization that would make this algorithm solvable in $$$O(n log n)$$$ time complexity?

          And thanks for your input.

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

            I guess that is still an incorrect transition. For example, let's look at another example:

            1

            5

            00111

            00000

            Let's only focus on the first row of the matrix:

            0 0 1 1 1

            d's first row should look like the following:

            0 0 1 2 2

            I don't think we can efficiently calculate matrix d, without the observation described in the editorial.

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

              To be honest, I struggled to understand the solution described in the editorial, so I tried to come up with an alternative solution, building on the thoughts I had during contest, but I froze when I reach the fft form in my solution, and because I did not reach that topic yet, I tried to get insight through comments.

              Back to the example you provided:

              1
              5
              00111
              00000
              

              We would have the grid:

              0  0  1  1  1
              0  0  1  1  1
              0  0  1  1  1
              0  0  1  1  1
              0  0  1  1  1
              

              Therefore, the area partition would be:

              0  0  1  2  3
              0  0  1  2  3
              0  0  1  2  3
              0  0  1  2  3
              0  0  1  2  3
              

              Meaning the answer should look like:

              vector<int> height = { 5 };
              vector<int> width = { 2, 1, 1, 1 };
              
              depth | list of areas | total cost |
              0     | h[0] * w[0]   | 10 * 0 = 0 |
              1     | h[0] * w[1]   | 5 * 1 = 5  |
              2     | h[0] * w[2]   | 5 * 2 = 10 |
              3     | h[0] * w[3]   | 5 * 3 = 15 |
              total |               |         30 |
              

              Which makes perfect sense to me, is there anything wrong with this?

              In addition, I would really like to have your say on the fft matter, It is practically the main purpose of the comment. Thanks for your input.

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

                I made a mistake in my last response, here is another testcase:

                1

                5

                11110

                00000

                matrix d:

                1 1 1 1 2

                1 2 2 2 3

                1 2 3 3 4

                1 2 3 4 4

                1 2 3 4 4

                It can't be described by 2 vectors height, weight. If any matrix could be described in such a way, your solution could indeed be optimized with fft.

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

                  Thanks for the confirmation, I'll demonstrate my approach for the example provided:

                  1
                  5
                  11110
                  00000
                  

                  Area partition:

                  1  1  1  1  0
                  1  1  1  1  0
                  1  1  1  1  0
                  1  1  1  1  0
                  1  1  1  1  0
                  
                  1  2  3  4  4
                  1  2  3  4  4
                  1  2  3  4  4
                  1  2  3  4  4
                  1  2  3  4  4
                  

                  Calculations:

                  // note: zero is for the lost 0 region
                  vector<int> height = { 0, 5 };
                  vector<int> width = { 1, 1, 1, 2 };
                  
                  depth | list of areas | total cost  |
                  1     | h[1] * w[0]   | 5 * 1  =  5 |
                  2     | h[1] * w[1]   | 5 * 2  = 10 |
                  3     | h[1] * w[2]   | 5 * 3  = 15 |
                  4     | h[1] * w[3]   | 10 * 4 = 40 |
                  total |               |          70 |
                  

                  Which makes perfect sense to me.

                  Again, I'm grateful you helped me confirm my assumptions, I hope you have a wonderful day/night!

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

I tried to hack one solution to the problem H, but got "Unexpected verdict"

For example, hack number 1142852

UPD: fixed :)

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

Thanks for the nice contest! Indeed, I believe G was more approachable than F, but maybe that's just because I'm bad xd

Anyways, there is an alternate $$$O(n)$$$ solution to F (then again, you could argue that the main solution is also solvable in $$$O(n)$$$ with counting sort since the differences are at most [-2*n, 2*n]) which is achievable even if you forget that math exists (happened to me).

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

    Can you Help me with some tips for thinking while problem solving, when I was solving F, I was immediately able to recognize that grid represented 'b' string row wise and 'a' string column wise and I wrote down the matrix for last testcase, also an intuition crossed my mind that on a path minimum of count of (0 and 1) will be the answer. But this was initial vague idea then i started thinking along lines of DP trying to formulate transitions from 0->1 , 0->0 , 1->0 and 1->1. (Basically got lost), so what could i have done differently?

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

      Ok, so, there was this really nice blogpost written some time ago. There, they mentioned that often times it can be useful to pretend that you're inside that world. That is to say, imagine you had to move over the board to reach a certain $$$(x, y)$$$.

      This line of reasoning can help you understand that, as an example, if we need to reach $$$(x, ?)$$$, then it never makes sense to first go to some $$$(x + 1, ?)$$$ so as to get a better answer. After that, if you pretend to move across the board, then you could notice the fundamental idea that if we ever go from a certain parity to a different parity value (like, from 0 to 1 or from 1 to 0) on a certain column, we'd always be forced to change some value. Therefore, we arrive to the idea of using the minimum of the zeros and ones.

      Basically, pretending that you're in that world can help sometimes.

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

can someone please tell me whats wrong in my solution for E. i feel like it follows the idea on the editorial, which is changing the values of a[i] such that a[i]^a[i+1] == b[i], and then change the values of a[i] such that a[i]^b[i+1] == b[i].

333476770

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

    Since == operator has higher precedence than ^, you have to have the a[i]^a[i+1] part inside brackets. Otherwise it'll evaluate as a[i]^(a[i+1]==b[i])

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

lol I literally did prob a by simulating the operations and passed

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

I've tried to implement a stupid solutions was: "Random 4 positions and repeat it 100 times", of course, it did not work :))))))

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

Had a clean solution for E. It follows this logic:

Finds the x such that a_i ^ x = b_i, if x != a_i+1 and x != b_i+1, impossible. a_n-1 must equal b_n-1 as well. Now, we can do a left to right sweep making all a_i which depend on a_i+1 ok. Then, do a right to left sweep which makes all a_i which depend on b_i+1 being ok which happens only if a_i+1 == b_i+1, i.e, i+1 is ok.

333481914

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

My rating prediction is far away from real estimation, plz ignore it XD

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

C and D is good! D tells us that we should understand the sample test better when we solve problems.(Chinglish)

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

I think E is easier than D.

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

i didn't realize Div3 is unrated for me untill i finish D. haha Then i turn to H. H is really a good problem

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

Can anyone please share some tips for problem like C. I have learn a lot of data structures and algorithms like segment tree or mst and already solved some problem that have higher rating than this, like over 1500. But these kind of problem always make me feel frustrated.

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

    I proved it mathematically first then coded it i dont know but it could help, it helped for me but i use unordered map which made it fail, should have used map. proving something mathematically helps a lot

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

F is such a beautiful maths problem, loved it!!

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

Guys did ratings change for y'all?

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

Thanks to -firefly- for maintaining Div-3 standard

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

We can calculate the sum (problem F) in a different way:

The sum is $$$\sum_{i,j} (\text{if } c_{0,0,i}+c_{0,1,j} \geq c_{1,0,i}+c_{1,1,j} \text { then } c_{1,0,i}+c_{1,1,j}\text{ else } c_{0,0,i}+c_{0,1,j})$$$.

We can transform the condition to $$$\sum_{i,j} (\text{if } c_{0,0,i}-c_{1,0,i} \geq c_{1,1,j} - c_{0,1,j} \text { then } c_{1,0,i}+c_{1,1,j}\text{ else } c_{0,0,i}+c_{0,1,j})$$$.

Now, we define $$$d_i = c_{0,0,i}-c_{1,0,i} \text{ and }e_i = c_{1,1,i} - c_{0,1,i}$$$.

It seems like $$$d$$$ is just $$$prea$$$ and $$$e$$$ is just $$$preb$$$ in the editorial.

After that it's enough to do 2 pointers.

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

Where can i report cheaters? anyone has any idea ?

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

I totally agree with efishel's rating prediction.

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

In D's editorial, can anyone explain this statement — "For vertices with depth larger than 1 , notice that in every operation, only one of the vertices on the path selected can be a leaf."

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

    I think the author means that the path should include a leaf because it's the best choice. And since it's a simple path originating at the root, it admits a single leaf. Note that the answer would be different if we could choose any vertex on the path as the common target for the operation, as the path would admit two leaves. Hope this makes sense.

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

      Pls correct me if I am wrong but why can't we choose a path starting and ending at a leaf? For eg the diameter of a tree? Why do we have just one leaf in a path?

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

        Because it wouldn't be the best choice. Consider a star-shaped tree. In this case, if you operated on a path with two leaves, one of them would receive the other as neighbor and the tree diameter would increase. Even if you repeated this process while the diameter is not minimal, you'd waste operations. You may think of it as an "unstable" kind of operation.

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

I wrote the below code for problem C, but it said TLE on test case 10. My code is linear and I used a Counter. I know hash collisions are there but is it really that bad for 10⁵ elements?

from collections import Counter
for _ in range(int(input())):
    n,k=map(int,input().split())
    arr=list(map(int,input().split()))
    brr=list(map(int,input().split()))
    pos=[i%k for i in arr]
    ctr=Counter([i%k for i in brr])
    for i in pos:
        if ctr[i]: ctr[i]-=1
        elif ctr[(-i)%k]: ctr[(-i)%k]-=1
        else:
            print('NO')
            break
    else: print('YES')
»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anybody tell me why did I get TLE on this?? 333331424 It got submitted in first try, but now during systems testing I got TLE

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

Wally West speed editorial

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

Spent too much time on D

Can someone suggest more problems like D?

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

I got a time limit exceeded error on problem C although I believe my solution is O(n). Other valid submissions are also O(n). Can anyone tell me what went wrong?

333372793

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

I am getting wrong answer on test 5 in G. This is my submission link 333433429.

Can anyone pls help !!

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

my code to c worked during the contest but in testing it says TLE anyone knows the reason or have same issue?

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

Someone please tell me why when I using same code with unordered_map it showing tle but not with map for problem C (333565895)

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

Has anyone rating been updated or not? It's been 35 hours, and my rating still hasn't been updated.

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

Is using a hash table for Problem C gonna get hacked?"

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

    Generally, when you use a hash table, you risk $$$100\%$$$ danger being hacked during a Div. 3, Div. 4, or Educational round.

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

Bonus for E: solve for without the condition of 'at most one operation' i.e the operation of taking xor with next index can be done any number of times. I missed 'at most once' and solved the harder question, ultimately costing me the question as there was less time left for correct solution.

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

For problem D, I didn't understand how to test all possible roots and paths in O(n), I tried to select the node with max edges count because it would make shorter paths, but I get wrong answer, can anyone help me?

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

Can anyone tell me what’s wrong with this solution https://mirror.codeforces.com/contest/2131/submission/333322773 compared to this one https://mirror.codeforces.com/contest/2131/submission/333639039 ? The first gave me a runtime error, but I don’t know why.

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

Who gives a Tree Problem in Div3D??????

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

this editorial is very good

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

Why is there a Detective Conan ref in the problems bro

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

What is the problem with my solution for Problem C? Can anyone please tell why it is giving incorrect result for test 2?

333731906

#include <bits/stdc++.h>

using namespace std;

bool can_convert(long long a, long long b, long long k)
{
    if (k == 0)
    {
        return (a == b);
    }

    if (a == b)
    {
        return true;
    }

    long long p = a + k;
    long long q = llabs(a - k);

    return (p <= b && (llabs(b - p) % k) == 0) || (q <= b && (llabs(b - q) % k) == 0);
}

int main()
{
    int t(0), n(0);
    long long k(0);

    cin >> t;

    while (t-- > 0)
    {
        cin >> n >> k;

        if (k == 1)
        {
            cout << "YES" << '\n';
            continue;
        }

        multiset<long long> s;
        multiset<long long> t;
        int in;

        for (int i = 0; i < n; i++)
        {
            cin >> in;
            s.insert(in);
        }

        for (int i = 0; i < n; i++)
        {
            cin >> in;
            t.insert(in);
        }

        for (long long a : s)
        {
            for (auto it = t.begin(); it != t.end();)
            {
                if (can_convert(a, *it, k))
                {
                    it = t.erase(it);
                    break;
                }
                else
                {
                    ++it;
                }
            }
        }

        if (t.size() == 0)
        {
            cout << "YES" << '\n';
        }
        else
        {
            cout << "NO" << '\n';
        }
    }
    return 0;
}
»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone give me link to problems like C and can also share tips on how to approach these type of problem. Some kind of general technique or pattern you see on these type of problem.

It will be very helpful, as i am struggling in these type of probelms.

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

Why TLE in 12th Test case ? is it because i am using unordered_map ?

submission

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

Try solving problem E but instead of one operation you get infinite :)

Hint
Solution
Code
»
9 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Can someone help me explain why I used function dfs in pypy3 but got RE in problem D ? https://mirror.codeforces.com/contest/2131/submission/333368249

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

G and H are probably the most beautiful d3 problems I've ever solved! (H with edi). This contest is definitely miles ahead of regular d3s in terms of quality. Loved it!

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

can someone please explain solution for problem F, like used by @jiangly submission

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

D is so simple (and nice!) yet I was scared by the need to choose the root myself. I have learned something. Thank you for the contest!

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

I still can't get how the summation in problem F works,

we are essentially trying to find ∑ |f(x) — g(y) |, over x and y from 1 to n, for both. And f and g are some functions. The way I think this is supposed to work is you first go through all values of f(x) and g(y), and store them into 2 separate vectors fx and gy and then sort them, and then find their prefix sum, and then start with 2 pointers, let's say x and y, from 0, while(x<n && y<n) and then ans = 0; if(fx[x] < gy[y]){ ans+=(prefy[n] — prefy[y]) — ( (n-y) * fx[x++]); } since i can take difference of all values from current y till the end of y, for this current x.And also increment the x.

It would be a real help if someone could review this process.

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

    dang,I didn't knew code forces is going to remove the spaces.

    I hope this one comes out find.

    We are essentially trying to find ∑ |f(x) — g(y) |, over x and y from 1 to n, for both.

    And f and g are some functions. The way I think this is supposed to work is you first go through all values of f(x) and g(y), and store them into 2 separate vectors fx and gy and then sort them, and then find their prefix sum, and then start with 2 pointers.

    Let's say x and y, from 0, while(x<n && y<n)

    and then ans = 0;

    if(fx[x] < gy[y]){

    ans+=(prefy[n] — prefy[y]) — ( (n-y) * fx[x++]);

    }

    since i can take difference of all values from current y till the end of y, for this current x.

    And also increment the x.

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

Hello, my handle is sm_10.
My submission 333439059 for problem 2131F was flagged for plagiarism.
I want to clarify that I did not copy from other participants.

I used the prefix-sum + binary search approach after studying this publicly available StackOverflow discussion that predates the contest:
SOF-Link

The similarity with other solutions seems to come from the fact that this is a standard known technique. I did not share my code with anyone.

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

Why is there a discrepancy between the textual description of the solution to Problem F and the code? There is no content related to n * n * (n + 1). It would be really great if someone could reply to me.

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

Can someone explain C to me?

My solution TLE's

from collections import defaultdict

for t in range(int(input())):
    n, k = map(int, input().split())
    s = list(map(int, input().split()))
    t = list(map(int, input().split()))

    cnt = defaultdict(int)
    for i in range(n):
        s[i] = min (s[i] % k, k - (s[i] % k))
        t[i] = min (t[i] % k, k - (t[i] % k))

        cnt[s[i]] += 1
        cnt[t[i]] -= 1

    print("YES" if all(x == 0 for x in cnt.values()) else "NO")s

But I don't understand what is going on in the editorial solution with RD (a random 32 bit integer?)

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

As for problem F, it's really hard to come up with $$$\min(a, b) = \dfrac{a + b}{2} - \dfrac{|a-b|}{2}$$$ for people who haven't met a trick like this (like me)

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

A simpler solution/code for question 2131F - Unjust Binary Life {Question F unjust binary life}.

Essentially the same concept but an easier implementation without the minimum to average part 356146228

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

another very interesting way to solve G:

368307753 It is a bit slow but much shorter than the editorial solution

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

    I think this turns the problem into almost a full DP+observation problem