dope's blog

By dope, 2 days ago, In English

Thank you for participating! We hope you enjoyed our problems

2057A - MEX Table

Author and developer is dope

Solution
Implementation

2057B - Gorilla and the Exam

Author and developer is dope

Solution
Implementation

2057C - Trip to the Olympiad

Author is I_love_kirill22 and developer is dope

Solution
Implementation

2057D - Gifts Order

Author and developer is dope, also thanks mupp and KoT_OsKaR for discussing this problem with me

Solution
Implementation

2057E1 - Another Exercise on Graphs (Easy Version)

Author is I_love_kirill22 and developer is dope

Solution
Implementation

2057E2 - Another Exercise on Graphs (hard version)

Author is I_love_kirill22 and developer is dope

Solution
Implementation

2057F - Formation

Author is induk_v_tsiane and developer is dope

Solution
Implementation
Alternative solution

2057G - Secret Message

Author and developer is I_love_kirill22

Solution
Implementation

2057H - Coffee Break

Author and developer is I_love_kirill22

Solution
Implementation
Tutorial of Hello 2025
  • Vote: I like it
  • +45
  • Vote: I do not like it

»
2 days ago, # |
  Vote: I like it -80 Vote: I do not like it

first

C and D were cool imo

»
2 days ago, # |
  Vote: I like it +27 Vote: I do not like it

It looks like b has some typographical errors :)

  • »
    »
    2 days ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    which is display as"Unable to parse markup [type=CF_MATHJAX]".Is it me?

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

    will be fixed soon

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

      Please open up solution links too, currently trying to access the link throws "You are not allowed to view the requested page" error notification.

»
2 days ago, # |
  Vote: I like it 0 Vote: I do not like it

thank you for opening the new year!

»
2 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Also, we are unable to see the coded solutions.

»
2 days ago, # |
  Vote: I like it +15 Vote: I do not like it

Can anyone help me find out the cause of the difference of these submissions? 299626640 299629236

I'm pretty sure the codes are exactly the same, and on my local MSVC the outputs for the examples are also correct (same as C++23). I don't recognize any UB in my code, so I don't get why C++20 shows a wrong output while C++23 runs as my expectation.

  • »
    »
    46 hours ago, # ^ |
    Rev. 2   Vote: I like it +40 Vote: I do not like it

    Fascinating.

    I was able to simplify this down to something like the following (run in customtest under G++20 13.2, 64 bit)

    #include<iostream>
    
    int main()
    {
    	int l, r;
    	std::cin >> l >> r;
    //	l = 128, r = 132;
    	for (int i = 30; i >= 0; i--)
    	{
    		if (!!(l & (1 << i)) != !!(r & (1 << i)))
    		{
    		      std::cout << i << '\n';
    		      return 0;
    		}
    		if (l & (1 << i)) return 0;
    	}
    }
    

    This outputs 7 when 128 and 132 are given as input, and quits quietly if the line // l = 128, r = 132 is uncommented. Haven't seen anything like this before. It seems I should recall something from the very beginning of my journey, where someone once mentioned to me that bitwise operations with signed integers can lead to UB. However, I couldn't find any information about this online.

    It would be very interesting to hear from someone more experienced in C++ about this issue.

    UPD: Looks like this is a compiler bug. I shared the problem in a local chat, and they showed me the site. The file about C++ 20, parts 7.6.7 and 7.6.11 seem to define correctly bitwise operations both for signed and unsigned integers .

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

      Wow, so I guess it's high time that I completely move on to C++23. Thank you for the analysis.

    • »
      »
      »
      31 hour(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I found out something interesting :D

      #include<iostream>
      
      int main()
      {
      	int l, r;
      	std::cin >> l >> r;
      // 	l = 128, r = 132;
      
      	for (int i = 30; i >= 0; i--)
      	{
      	    
      	     int res = (!!(l & (1 << i)) != !!(r & (1 << i)));
      	     if (res > 0)
      	     {
      		  std::cout << res << " res\n";
      		  std::cout << i << "\n";
      		  return 0;
      	     }
      	     if (l & (1 << i)) return 0;
      	}
      	
      	return 0;
      }
      

      Output on G++20 13.2, 64 bit:

      0 res
      7
      
  • »
    »
    46 hours ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I put them in VS Code and no differences showed up on comparison. I am sure it has to related to the compiler difference. Because the same submission works in C++17 (299700872). Hope someone can actually answer why this is so.

    UPD: Seems Leo has found out the reason for this behaviour.

  • »
    »
    45 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Compiling with #pragma GCC optimize("O0") seems to fix this bug

»
2 days ago, # |
  Vote: I like it +1 Vote: I do not like it

I misread problem C and thought I had to maximize a ⊕ b ⊕ c. Anyone knows a solution to this problem instead of the original one?

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    idk i haven't tried yet can have edge cases: but my intution was: you can just convert the minimum to maximum to arrays of binary and rev it using tradition mod 2 method store it in separate arrays and reverse it where a has first 0 and b has first 1 is there start from there to make it in range convert all alternate signs of a and b to 0 and 1 and get it together and c can be anything in the range leaving a and b. All together i am saying where 0 of minimum and 1 of maximum in same index as from there you can change the upcoming array and just change a[i] where both have same digit to a[i]=1 and b[i]=0. then convert the array to a and b simultaneously you will always get a and b in the range .

  • »
    »
    47 hours ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it
  • »
    »
    46 hours ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Well I think it will be like take l and r : Write bit form of them , check the most significant bit of them which will be different and of course the "r" have the bit '1' while l will be '0' at that point (Let it call "A") . Now after that point of change set all the bits of "r" to zeroes and set the after bits of "l" to ones . Now the let numbers will be "a" and "b" ... thinking of third number see the xor of earlier two number ... it will be the continuous stream of 11111... from point A . If the third number came having the same bits earlier to "A" of l(or r, as they same) , so it will be in range to [l,r] , now choose the number such way the stream of 11111... is not disturbed which will be that the bit at point "A" will be "0" and if so then it will lies after "b".

    Case 1: If "b" is l : The number "c" will be out range : so we will choose the bit of point "A" be "1" and now the "c" will "a+1" as the last bit will be the "1" which will disturbed the bitstream (LSB one) .

    Case 2 : If "b" is not l : The "c" will be "l" . How? you can think it after all you are specialist

    • »
      »
      »
      46 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Example : 1)

      r: 1101100010

      l: 1101011111

      answer : a=> 1101100000 , b=> 1101011111

      b is l so c=> 1101100001

      xor =>1101011110

      Example : 2)

      r: 1101100010

      l: 1101011100 answer : a=> 1101100000 , b=> 1101011111

      b is not l so c=> 1101011100

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

    To maximizer a ⊕ b ⊕ c, we need to have max set bits possible, also keeping in mind that only allowed nos. can be in range [l,r].

    a-> 11110101
    b-> 11101100
    

    XOR both nos. and take the MSB. Here, MSB is 5th bit. Using this info we can generate 2 nos. whose xor will have all the set bits.

    2^4 = 16 & 
    (2^4)-1 = 15
    

    Now, further doing xor of 16⊕15 with any other no.(not having MSB greater than 5) other than 0 will reduce it.

    Hence, here max value of  
    a ⊕ b ⊕ c =  16 ⊕ 15 ⊕ 0 = 31
    

    So, max xor can be obtained with 2^k, 2^(k-1) and 0, where k is MSB.

»
2 days ago, # |
  Vote: I like it 0 Vote: I do not like it

I have been practicing questions from various sheets (CP-31 mostly) got recently introduced to themeCP.

i can solve 800-900 rated questions with little effort but 1000 rated problems do take up my time.

Can someone lend some resources that might help?

My problem -> I thought Solution of A at an instant but took time trying to prove myself that it will actually work (drawing various scenarios). Same happened with B.

Thanks in Advance for advices :)

  • »
    »
    2 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    myself also new to cp . also want some resources , and peer group to discuss .

    • »
      »
      »
      2 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I need peer group too lol. But i think post contest discussion on these blogs also helps me a lot (also forces me to spend more time on CF :P )

      • »
        »
        »
        »
        47 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I also need peer group, as I am from non-tech background

      • »
        »
        »
        »
        22 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        hey, can u pls tell how to watch the post contest discussions, like, how to see those streams after they ended as they arent visible once it ends.

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

    You do not need resources, you need practice.

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    learn basic algo like line sweep prefix sum binarysearch if you know these then practice i guess

    • »
      »
      »
      47 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      yeah but for later as a newbie implementation should be formost priority cuz this time also all A B C was completely implementation based :)

  • »
    »
    45 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    do CP-31 religiously, and learn from every problem there. I completed the entire 1600 rated list, and it opened gates to many new intuitions and thinking paradigms

»
2 days ago, # |
  Vote: I like it +16 Vote: I do not like it

Nice round. Thank you for the wonderful problems.

»
2 days ago, # |
  Vote: I like it -44 Vote: I do not like it

D should not be a segment tree, solving D in a 2.5hr contest with such easy ABC is the level of high specialist/low expert, and many people at this level don't know segment tree, or they know it at a basic level and don't have enough experience/understanding to solve this kind of segment tree problems where they require a not-so-obvious merging. the problems were good and were at the standard of a Big div1+2 contest, but the fact that D required segment tree and no other solutions existed in such an important contest is disappointing.

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    you can use sqrt decomposition then

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

    I agree with you. It is brutal to give a segment tree in a div2D. Maybe it would be more fair to split the task with the easy version not having queries. It would allow divide and conquer approaches.

    On the other hand, the number of solves is realistic. The difficulty is appropriate.

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

C was a very good problem. Thank you.

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

I learnt two things from this contest:

  1. I can figure out the logic/pseudocode for the question in ~10% of the time I spend on the problem when it should be the opposite. I struggle with writing the code for it.

  2. I suck at bitwise operations. Does anyone here have a good list of questions to practice to get good at basic problems (upto C level?)

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    do CP-31 sheet it will immensely help and for dsa you can follow striver sheet(topic wise)

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

C also can be solved by Ternary Search

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Could you explain your approach please :D.

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

      Yeah if you know two things

      First.You know how Ternary Search works?

      Second.Do you know that there is always a triple of numbers l,r and some c where l and r are given numbers?

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

        After watching a 4 min YouTube video, my current understanding is that Ternary search is similar to binary search except that we have two mid values in the search.

        As for your second point, I tried making a=r and b=l in the contest, and I kinda "guessed" that there must be a third value that would maximize the pairwise xor sum, and I thought of the 1's complement (starting from the MSB 1 bit of r), and it turned out to give the same pairwise xor sum as in the input example, except that I got values less that L sometimes, and I couldn't think of a better way.

        Forgive me for making a long comment.

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

          So you got how my approach works? If not, i'm sorry and could you tell me what you didn't get?Maybe authors approuch will be more understandable

          • »
            »
            »
            »
            »
            »
            46 hours ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            If I got it I would've not asked, but thanks anyways :D (please do not further reply).

          • »
            »
            »
            »
            »
            »
            27 hours ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            can you please explain your approach. i understand that taking two of the numbers as l and r is valid. but what i don't understand is how you reject 1/3rd of the possible values based on the value of the function at mid and mid2

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

can someone explain why my submission is wrong?

https://codeforces.net/contest/2057/submission/299685645

i did exactly what editorial says

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

at problem c you can choose a as being l , b as being r ,and c as being the value that maximizes the sum , so to calculate c you go decreasingly on bits and if the bits are present in both l and r you have to take it,if it's present in l we have to take it and if its not presesnt in l we just see what would happen if we took this bit and then choose the other smaller one greedily,overall complexity(30^2)

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I tried to find c ,but it fails at certain cases. What i thought was c would consists of all set bits that are unset in both a and b for example) if a = 6 and b = 22 , c would be 9.But this fails when c <= l. Can we find c using brute force ? if a is L and b is R.

    • »
      »
      »
      47 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
    • »
      »
      »
      46 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      i did something similar to find c.

      my solution: let k'th bit be the highest bit that is different in l and r. now, case 1: if all bits lower that k'th are opposite in l and r. you have the answer as a=l, b=r and any c such that l <= c <= r, other than a or b.

      Case 2: let p'th bit be the highest bit (< k'th) where we have same value set for l and r. here we can construct c as —

      for bits > k'th: use same bit value as l (or r).

      for bits <= k'th: if i'th bit is same in l and r. for c, set its i'th bit as negation of this value. if i'th bit is different in l and r — if p'th bit is 0 in l (or r), set i'th bit of c to be same as i'th bit of l. else, set i'th bit of c as i'th bit of r

      my solution: https://codeforces.net/contest/2057/submission/299666931

      • »
        »
        »
        »
        42 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        can you explain the last step ? i get it if its same we set its negation but what to do if its different

        • »
          »
          »
          »
          »
          34 hours ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          try to construct c for these two examples:

          r = 11000 l = 10110

          r = 11001 l = 10110

          the logic i mentioned above helps maintain c within the range [l, r]

»
47 hours ago, # |
Rev. 5   Vote: I like it +6 Vote: I do not like it

I cheesed C with a randomization algorithm.

It passed systests.

Please hack it.

https://codeforces.net/contest/2057/submission/299661239

V2: https://codeforces.net/contest/2057/submission/299698846

UPD: Hacked by MattTheNub

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    whats the main idea for randomization algo to work here ?

»
47 hours ago, # |
Rev. 2   Vote: I like it +27 Vote: I do not like it

For E you can forget the binary search altogether by processing queries offline.

For a query (a, b, k), everytime you change the distance d(i, j), check if d(i, j) < k, and the first time this happens, the answer to that query is the value of the edge you're adding.

By storing the lists of queries for each (a, b) sorted by k, you can process all these simply as you modify the graph, and you don't have to store the dp layers.

  • »
    »
    46 hours ago, # ^ |
      Vote: I like it +36 Vote: I do not like it

    With this offline approach we can optimize the E1 solution from editorial down to O(n^4/64) using bitsets, which easily fits in E2 time limit.

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

can so explain why my submission(problem b) is wrong?

https://codeforces.net/contest/2057/submission/299644013

»
47 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

Several python solutions from I've seen (including mine) for problem B have TLE'd on test case 17. What is the cause of this?

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Potential use of unordered_map in C++ or (Python) dictionary has led to hash collisions

    • »
      »
      »
      47 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      You mean they made special test for unordered_map? I always thought that unordered_map can be only like 5 times slower, if it's not some "special" test.

      • »
        »
        »
        »
        46 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Someone probably succesfully hacked someone by causing hash collisions, and all succesful hacks end up in system testing.

  • »
    »
    46 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I found that changing the keys of my dictionary (e.g. multiplying by a big constant) solved the issue.

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

Hmmm, IDK why I approached D the way I did, segment tree would've been much simpler and faster especially since I used the same logic for my solution.

However, it's weird that $$$O(n * \sqrt{n})$$$ struggled to pass. In the end, it squeezed in just enough to pass when I changed the block size from $$$\sqrt{n}$$$ to $$$550$$$ which is also weird. Any ideas?

Here's the submission 299683723

  • »
    »
    32 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    My submission is faster 299666269. Why do you use multiset?

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

      I honestly don't know, it just happened LOL

      Tho it's not adding to the complexity in big O, it's a heavy constant factor.

      It's still interesting that increasing the block size actually made it fit.

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

        you are aiming not for O(sqrt(n)* n * logn) but for O(sqrt(nlogn)) Sorry, my english very bad

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

          Can you explain further?

          because as I see it I'm aiming for O(n * (log(n) + sqrt(n))

»
47 hours ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

can anybody tell why it is giving TLE?(B problem) https://codeforces.net/contest/2057/submission/299631565

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

    Use map instead of unordered_map

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

      Wow, that's really silly. Isn't std::unordered_map supposed to be faster than std::map? Otherwise, what's the point?

      edit: apparently std::unordered_map is vulnerable to hash collision attacks, see this excellent post by neal: https://codeforces.net/blog/entry/62393 (so it's not that unordered_map is slower in general, it's just that it's vulnerable to specific hacks).

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

        Both map and unordered map have high constant factor and are very heavy structures.

        Also map is more consistent in complexity where it's log(n) * constant while unordered map have undefined behaviour which can make queries take O(n).

        I'm not sure if this is scientifically correct but that's what I learned from practice idk.

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

https://codeforces.net/contest/2057/submission/299695552

For b this shud work ryt it's just nlogn but tle for testcase 14

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You got it cause of the unordered_map (everyone who used unordered_map got TL, even python coders).

    • »
      »
      »
      46 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Got the same. Unordered_map for ints should be exactly O(1), shouldn't it?

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

Solution code for D doesn't match the editorial. Editorial says that it's enough to store answer, minimum and maximum of $$$a_i - i$$$, but in the solution there are some $$$max1, max2, min1, min2$$$ stored in the node

  • »
    »
    46 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    If you observe carefully, we can create 2 separate test cases to be solved:

    1. In the range (l, r), al is the minimum element, and ar is the maximum element. This can be solved by changing the array to: ai-i, and final answer be given as (ar-r)-(al-l).

    2. In the range (l, r), al is the maximum element, and ar is the minimum element. This can be solved by changing the array to ai+i, and final answer be given as (al+l)-(ar+r).

    This 2 cases can be modeled in separate segtrees, or storing the information in the same segtree.

    • »
      »
      »
      45 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yep, I've upsolved it already, just pointing out an incorrect editorial

    • »
      »
      »
      21 hour(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Can you explain how you implemented this? In particular, after you build the two segtrees, how do you query the answer? I think it's just querying the max difference between two elements where the max element appears after the min one (or vice versa), but not sure how to code this.

»
47 hours ago, # |
  Vote: I like it +5 Vote: I do not like it

Why using unordered_map in problem B gave TLE on testcase 14!!!!

Replacing it with map worked...

my solution passed prestests, but failed in system testing.. this is awful

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

    Because unordered_map is not implemented in the best way in C++.

    You can read more about that here

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

      thanks..

      I already found this amazing post and was reading it and it seems to work.

      Does it mean that we should always use map in cpp in competition?

      While using unordered_map with custom hash worked for problem B, it doesn't seem to be faster than the map implementation in any sense

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

        Solutions using default unordered_map in c++ generally get hacked because the hash function is fixed and specific test cases could be made in order to create lots of collisions which changes the find tc to O(n) from O(1). For a random custom hash, specific test cases for collisions can't be created.

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

In Python, I feel very comfortable, but I don't have the same ease with C++, and I’m not sure how to achieve it—I guess it’s just a matter of practice. This was my third contest, and I managed to solve the first two problems using Python. However, during the system testing, the second problem resulted in a time limit exceeded, even though my algorithm was correct :(. Any advice on transitioning to C++?

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

    You got TL not because you used python, but because you used dictationary. Potential use of unordered_map in C++ or (Python) dictionary has led to hash collisions.

    • »
      »
      »
      39 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Does this not affect HashMaps in Java?

    • »
      »
      »
      26 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I understand. But, now taking in general, is it possible to solve all the problems in python with the same algorithm that are solved in C++?

      • »
        »
        »
        »
        20 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        A lot of tasks (usually starting with div2D) cannot be solved in Python, but they can be done in C++. Sorry my english is very bad

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

For E2, using DP and binary search is overcomplicated. We don't need to use either by processing the queries offline, resulting in much easier code.

https://codeforces.net/contest/2057/submission/299696265

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

Performance Query: Codeforces Problem

Description

I implemented two similar solutions for a problem, but I’m encountering unexpected behavior regarding their performance.

1st Code

299628021

for _ in range(int(input())):
    sz , k = map(int,input().split())
    arr = list(map(int,input().split()))
    freq = dict()
    for a in arr:
        a = str(a)
        if (a not in freq):
            freq[a] = 1
        else:
            freq[a] += 1
    final = sorted(freq.values())
    ans = len(final)
    for a in final:
        if (k-a >= 0):
            ans -= 1
            k -= a
        else:
            break
    print(max(ans,1))
  • Converts each element of the array to a string using a = str(a) before processing it in the freq dictionary.
2nd Code

299696582

for _ in range(int(input())):
    sz , k = map(int,input().split())
    arr = list(map(int,input().split()))
    freq = dict()
    for a in arr:
        if (a not in freq):
            freq[a] = 1
        else:
            freq[a] += 1
    final = sorted(freq.values())
    ans = len(final)
    for a in final:
        if (k-a >= 0):
            ans -= 1
            k -= a
        else:
            break
    print(max(ans,1))
  • Directly uses the elements as integers without converting them to strings.
Observation
  • 1st Code: Runs successfully without TLE, even for larger test cases.
  • 2nd Code: Gives TLE at test case 17.
Expectation

I expected the 2nd Code to be faster because: 1. It avoids the overhead of string conversion (str(a)). 2. Integer keys in dictionaries should perform better due to faster comparisons and reduced memory usage.

Confusion

Why is the 1st Code faster in this case?
Is there something specific about the input data or Python’s dictionary implementation that might favor string keys over integer keys?
Any insights into this would be greatly appreciated. Thank you!

»
47 hours ago, # |
Rev. 2   Vote: I like it +26 Vote: I do not like it

About E1. Why is E1 in the set when the constraints are so tight that some intended complexity solutions don't pass? Mine was $$$O(n^3 logn)$$$ takes 5600ms in the mashup, 4200ms in C++20), which is fine not to pass, in my opinion. But some $$$O(n^3)$$$ solutions are not passing as well, for example (funnily enough, this passes on C++23). For me, this just led to spending a lot of time trying to optimize constants (an almost correct time complexity) instead of thinking about how to solve E2.

Also, the constraints were not even tight enough to kill bitset, so I am curious why $$$n = 400$$$ was chosen.

  • »
    »
    46 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Well, mine was kinda similiar to the TLE submission but yeah, I did get pass even with C++17. I guess it's about the implementation now.

  • »
    »
    41 hour(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    could you please explain you code https://codeforces.net/contest/2057/submission/299668054 especially why is the dfs done in such a weird way

  • »
    »
    38 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    My $$$O(n^3\log n)$$$ sol passed both E1 and E2 (E2 needed some constant optimization though)

    • »
      »
      »
      36 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      What was your $$$O(n^3\log{n})$$$ idea?

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

        299676764 I did some research and it turns out that the complexity is indeed $$$O(n^4)\sim O(n^4\log n)$$$ but the constant is too small to be hacked ... so forget about it (maybe someone else can construct a stronger test)

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Got TLE for B with two approaches: 299696692 299635098 What could be wrong?

  • »
    »
    46 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Unordered_map; use map instead

  • »
    »
    46 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It's TLE because you used unordered_map instead of map. Here is your code accepted just by changing unordered_map to map: 299701332

    You can read more on the problem here: https://codeforces.net/blog/entry/62393

    In the future just use map instead of unordered map, unless you absolutely need unordered map.

»
46 hours ago, # |
  Vote: I like it +13 Vote: I do not like it

Sadly, nobody got 2025 rank in today's contest :( .

»
46 hours ago, # |
  Vote: I like it +12 Vote: I do not like it

In the editorial for E1 it says

d′[i][j]=min{d[i][j],d[a][i]+d[b][i],d[b][i]+d[a][i],}

shouldnt it be d'[i][j]=min(d[i][j],d[a][i]+d[b][j],d[b][i]+d[a][j]}?

»
46 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

299701558

Hey, could anyone figure out why my solution TLEs? It runs in O(nlogn) time with the single sort, which is the same complexity as the solution.

»
46 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

I propose a solution for 3rd one.. it seems simpler to me.. please give your opinions -

  1. Take a = l, b = r.
  2. Find the 1st different bit in l and r.
  3. Then take complement of rest of part of l and r and store resulting number in cl and cr respectively.
  4. Now if cl < r, c = cl, Else if cl = r, c can be any number between l and r Else c is cr.
  • »
    »
    41 hour(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Does this AC though?

  • »
    »
    30 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    But why take a = l and b = r? What's the proof?

    • »
      »
      »
      24 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      ughh.. it's because partial complement of at least one of l or r lies between l and r and the third number can be anything to maximise the some so we chose it to be the remaining of l and r. Proof of this is simple, try urself( I'm too lazy to explain the whole thing :( )

»
45 hours ago, # |
  Vote: I like it -20 Vote: I do not like it

Wtf segment tree problem in D? It is bad

»
45 hours ago, # |
  Vote: I like it +13 Vote: I do not like it

My code 299683214 passed on pretests and gave tle on final tests, and now it is again passing 299704642.

Can it be reviewed? I_love_kirill22 induk_v_tsiane dope

»
45 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone help me figure out where I go wrong in my solution ? https://codeforces.net/contest/2057/submission/299683979 I basically try to maintain the maximum, the minimum, and their respective indexes, along with the value of a segment for max difference. The value becomes something like max(left value, right value, abs (left max — right min) — abs (l1-r1) , abs(left min — right max) — abs (l2-r2)) where li ri are indexes of the maximum and minimum

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

I had a different solution for E1/E2, maybe it is easier to understand for some people.

E1: For every query, check if each edge can be the answer. To do so efficiently, for every edge going from u to v with weight w, precompute the the arrays udist and vdist, where udist[x] equals minimum # of edges with weight >= w needed to go from u to x, and same for vdist[x]. This can be done with 2M djikstras, so complexity of precomutation is O(M^2 log N). Checking if an edge can be the answer can be done in O(1) by checking if udist[a] + vdist[b] < k or udist[b] + vdist[a] < k. Total complexity is O(M^2 log N + MQ). MQ part might be bad complexity but it is just some array lookups so very low constant factor. Submission: 299661500

E2: Same thing as E1 except observe that the edge of the answer must be on the MST. This means for every query you only need to check O(N) edges. You also only have to do the precomputation step for O(N) edges. Use djikstras on dense graphs (O(N^2 + M) = O(N^2)), so complexity of precomputation is O(N^3). Total complexity is O(N^3 + NQ). Submission: 299706362

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

    Look at this test for problem E1:

    1
    9 12 1
    1 2 100
    2 3 100
    4 5 100
    5 6 100
    1 4 100
    4 7 100
    2 5 100
    5 8 100
    3 6 100
    6 9 100
    7 8 1
    8 9 1
    1 3 3
    

    model solution prints "1" but answer and your soluton is "100". Am I wrong?

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

Look at this test for problem E1:

1
9 12 1
1 2 100
2 3 100
4 5 100
5 6 100
1 4 100
4 7 100
2 5 100
5 8 100
3 6 100
6 9 100
7 8 1
8 9 1
1 3 3

main and tourist prints "1" but answer is "100". Am I wrong?

  • »
    »
    43 hours ago, # ^ |
      Vote: I like it +29 Vote: I do not like it

    The input must guarantee that any path from vertex $$$a$$$ to vertex $$$b$$$ contains at least $$$k$$$ edges. In your case, the path $$$1 - 2 - 3$$$ contains only $$$2$$$ edges, which is less than $$$k$$$.

»
40 hours ago, # |
Rev. 2   Vote: I like it -8 Vote: I do not like it

I'm curious about why my submissions got TLE:

E1: https://codeforces.net/contest/2057/submission/299716714 I also see one of my friend's submission, he wrote the same thing as me, but he got accepted. https://codeforces.net/contest/2057/submission/299642675

If it's because of my large constant, I don't think this is fair.

»
38 hours ago, # |
  Vote: I like it -14 Vote: I do not like it

ABC were too easy, but D was an interesting segment tree problem

»
38 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem C editorial:

Why do bits more significant than kth bit do not affect the outcome?

Why does taking x as divisible by 2^k , x-1 & any number not these 2 all in the range [l, r] give maximum answer.

The first paragraph is pretty clear, but how it relates to the next 2 paragraphs is not.

»
37 hours ago, # |
Rev. 2   Vote: I like it +11 Vote: I do not like it

E2 can be solved in O(N^3) without the extra term of qlogN by precomputing answers to all possible queries on the go — https://codeforces.net/contest/2057/submission/299682395

This allows to answer any number of queries (say 5e7) in the given TL.

»
37 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

I think B is easier than A cuz it only requires implementation.

»
36 hours ago, # |
  Vote: I like it -8 Vote: I do not like it

solved B passed pre test woke up to tle due to using erase method in vector . I dont like this pre test thing

»
35 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Someone explain C like I am 8 years old.

»
34 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

This Contest was Div1 or Div2? How do we get to know if a contest is div 1 or div2 if not mentioned in contest name?

  • »
    »
    16 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    In this case, the contest announcement would clarify the rating.
    For example, in this contest, they said "The round will be rated for all participants."

»
34 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

In Problem B, using unordered_map passed the pretests but caused TLE on test 11. Switching to map resolved the issue, and the solution was accepted. Can someone explain why ?

submission with unordered_map : https://codeforces.net/submissions/phoenix_vasu# submission with map : https://codeforces.net/submissions/phoenix_vasu# The only difference between these 2 code is unordered_map and map

»
33 hours ago, # |
  Vote: I like it +12 Vote: I do not like it

I solved D by finding maximum subarray sum on the difference array

  • »
    »
    27 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    can you elaborate how you did it using difference array and why used difference array as such ?

    • »
      »
      »
      21 hour(s) ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      First, think a descending array. Since the first element is the maximum you need to take it and while you are taking the elements one by one by iterating to the right, the minimum will decrease as the difference, and since you took one new element the number of elements you have chosen will increase and your answer will decrease by 1. So, when you select a new element your answer increases by (difference-1).

      To generalize, when you select the subarray with maximum sum the left and right borders of it are the maximum and the minimum element in the optimal subarray.

      The maximum can be the left border and the right border so to cover it I found the maximum subarray sum in the difference array by subtracting 1 from every element, and I also found the maximum subarray sum by complementing its elements and subtracting 1 from them. We need to subtract 1 from the elements because when you select one more element you need to subtract 1 because the score is (maximum-minimum-number_of_elements) I think you may better understand by trying some cases on the paper. Please feel free if you have a question.

»
33 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Why did the problem 2057B - Gorilla and the Exam I cannot use unordered_map, I got TLE when using using it

»
33 hours ago, # |
  Vote: I like it +42 Vote: I do not like it

ML of G is too tight I think. The 2e6 data range and the 256 MB space literally made no sense.

»
32 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone explain to me, why using "arr = [int(x) for x in input().split()]" instead of arr = input().split() results in execution time becoming more than 10 times slower.

299755424 --> slower one 299754131 --> faster one

  • »
    »
    28 hours ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    because python is stupid language

    • »
      »
      »
      27 hours ago, # ^ |
      Rev. 4   Vote: I like it +4 Vote: I do not like it

      I take it back =) This problem occurs in all languages and is caused by the peculiarities of hash tables

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

    The issue is not the code that you quote in your comment, but rather that you then add these values to a Counter. If you add them as an int, this is subject to hash exploits, more info here for example. However inserting them as str avoids these issues. This is a common reason for TLEs, and (almost?) everybody comes across this issue at some point. The aforementioned blog briefly discusses how to avoid getting hacked.

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

there is a mistake in E1: $$$\textrm{d}'[i][j] = \min { \textrm{d}[i][j], \textrm{d}[a][i] + \textrm{d}[b][\not{i} \ j], \textrm{d}[b][i] + \textrm{d}[a][\not{i}\ j], }$$$

upd: it fixed

»
27 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

I have done Problem C with a idea that l and r can be contained in an answer,and after that I use dfs to find another integer,and it gets ac,could anyone please tell my why this ieda works well?

»
27 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Has anyone Happy New Year! question D without segment tree? For example, with sqrt?

»
26 hours ago, # |
  Vote: I like it +5 Vote: I do not like it

this is not allowed ,this user is hiding his submissions .

https://codeforces.net/contest/2057/submission/299609972

dope

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

    The guy (maybe) doing something fishy but that is some cool shit.

»
26 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

During the round I got WA3 299664488. However my solution was failing due to OOB, and codeforces returned WA, instead of RTE, here is a fixed version 299794827. I was breaking my head for it :(.

»
24 hours ago, # |
  Vote: I like it -14 Vote: I do not like it

I am surprised you have 2100+ rating but why you are still at pupil , is there an error on CF ?

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

How to reduce the time complexity from O(E^2) where E = n(n-1)/2 on problem E2

My submission

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

For the solution code for D, what is the purpose of subtracting $$$n-1$$$ from the second value of the pair?

The code still gets accepted when I replace

t[i] = {a[i] - i, a[i] + i - n + 1};
...
t[p] = {x - p, x + p - n + 1};

with

t[i] = {a[i] - i, a[i] + i};
...
t[p] = {x - p, x + p};

Seems like an unnecessary operation, unless I'm missing something.

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

    It may be more intuitive for some people, as $$$x+p-n+1$$$ effectively means "take the reverse index of the position (that being $$$n-1-p$$$), and subtract from $$$x$$$, making this in some sense the same thing as $$$x-p$$$, just in reverse. It doesn't change the solution, as it's a constant change for each value in the array and you always take the difference of $$$2$$$ values in the array.

»
6 hours ago, # |
  Vote: I like it -14 Vote: I do not like it

won't the E1's solution fail at the below testcase. It should be giving 11 as the answer but is giving 1.

9 9 1
1 2 11
2 3 12
3 4 13
1 5 14
5 6 15
6 7 16
7 8 1
8 9 17
9 4 18
1 4 4
  • »
    »
    6 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The problem statement guarantees that for every query $$$(a, b, k)$$$, any path from $$$a$$$ to $$$b$$$ contains at least $$$k$$$ edges. In your given test case, this isn't satisfied: you have a query $$$(1, 4, 4)$$$, yet there is a path from $$$1$$$ to $$$4$$$ using only $$$3$$$ edges ($$$1-2-3-4$$$). Therefore, your given test case isn't valid.