Блог пользователя dope

Автор dope, 3 дня назад, перевод, По-русски

Спасибо за участие в раунде! Надеемся, что задачи вам понравились!

2057A - Таблица MEX

Автор и разработчик dope

Решение
Реализация

2057B - Горилла и зачет

Автор и разработчик dope

Решение
Реализация

2057C - Поездка на олимпиаду

Автор I_love_kirill22, а разработчик dope

Решение
Реализация

2057D - Заказ мерча

Автор и разработчик dope, также спасибо mupp и KoT_OsKaR за обсуждение со мной этой задачи

Решение
Реализация

2057E1 - Очередное упражнение на графы (простая версия)

Автор I_love_kirill22, разработчик dope

Решение
Реализация

2057E2 - Очередное упражнение на графы (сложная версия)

Автор I_love_kirill22, а разработчик dope

Решение
Реализация

2057F - Линейка

Автор induk_v_tsiane, а разработчик dope

Решение
Реализация
Алтернативное решение

2057G - Секретное сообщение

Автор и разработчик I_love_kirill22

Решение
Реализация

2057H - Перерыв и кофемашины

Автор и разработчик I_love_kirill22, спасибо FairyWinx за оказанное вдохновление

Решение
Реализация
Разбор задач Hello 2025
  • Проголосовать: нравится
  • +51
  • Проголосовать: не нравится

»
3 дня назад, # |
  Проголосовать: нравится -85 Проголосовать: не нравится

first

C and D were cool imo

»
3 дня назад, # |
  Проголосовать: нравится +27 Проголосовать: не нравится

It looks like b has some typographical errors :)

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

thank you for opening the new year!

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Also, we are unable to see the coded solutions.

»
3 дня назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится

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.

  • »
    »
    3 дня назад, # ^ |
    Rev. 2   Проголосовать: нравится +40 Проголосовать: не нравится

    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 .

    • »
      »
      »
      3 дня назад, # ^ |
        Проголосовать: нравится +8 Проголосовать: не нравится

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

    • »
      »
      »
      3 дня назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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
      
  • »
    »
    3 дня назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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.

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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?

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 .

  • »
    »
    3 дня назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
  • »
    »
    3 дня назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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

  • »
    »
    2 дня назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 :)

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      3 дня назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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 )

      • »
        »
        »
        »
        3 дня назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

      • »
        »
        »
        »
        2 дня назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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.

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится +7 Проголосовать: не нравится

    You do not need resources, you need practice.

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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

»
3 дня назад, # |
  Проголосовать: нравится +16 Проголосовать: не нравится

Nice round. Thank you for the wonderful problems.

»
3 дня назад, # |
  Проголосовать: нравится -44 Проголосовать: не нравится

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.

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    you can use sqrt decomposition then

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

C was a very good problem. Thank you.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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?)

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

C also can be solved by Ternary Search

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Could you explain your approach please :D.

    • »
      »
      »
      3 дня назад, # ^ |
      Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

      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?

      • »
        »
        »
        »
        3 дня назад, # ^ |
        Rev. 3   Проголосовать: нравится +6 Проголосовать: не нравится

        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.

        • »
          »
          »
          »
          »
          3 дня назад, # ^ |
          Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

          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

          • »
            »
            »
            »
            »
            »
            3 дня назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

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

          • »
            »
            »
            »
            »
            »
            2 дня назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            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

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

can someone explain why my submission is wrong?

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

i did exactly what editorial says

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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)

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.

»
3 дня назад, # |
Rev. 5   Проголосовать: нравится +6 Проголосовать: не нравится

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

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится +27 Проголосовать: не нравится

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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

»
3 дня назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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?

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      3 дня назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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.

      • »
        »
        »
        »
        3 дня назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

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

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      2 дня назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится

      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.

      • »
        »
        »
        »
        2 дня назад, # ^ |
          Проголосовать: нравится +3 Проголосовать: не нравится

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

        • »
          »
          »
          »
          »
          36 часов назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Can you explain further?

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

»
3 дня назад, # |
Rev. 3   Проголосовать: нравится +5 Проголосовать: не нравится

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

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    Use map instead of unordered_map

    • »
      »
      »
      47 часов назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      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).

      • »
        »
        »
        »
        36 часов назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

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

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.

    • »
      »
      »
      3 дня назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      2 дня назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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.

»
3 дня назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

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

    You can read more about that here

    • »
      »
      »
      3 дня назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится

      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

      • »
        »
        »
        »
        3 дня назад, # ^ |
          Проголосовать: нравится +4 Проголосовать: не нравится

        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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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++?

  • »
    »
    3 дня назад, # ^ |
    Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

    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.

    • »
      »
      »
      3 дня назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Does this not affect HashMaps in Java?

      • »
        »
        »
        »
        18 часов назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I'm not familar with java, but if it is based on hash, then the problem might happen.

    • »
      »
      »
      2 дня назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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++?

      • »
        »
        »
        »
        2 дня назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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

      • »
        »
        »
        »
        18 часов назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Most of them can be solved in python, but some very difficult problem not.That because python is too slow, and if allow python solve it, it might also allow some C++ solution with wrong time complexity pass it.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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!

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится +26 Проголосовать: не нравится

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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится +12 Проголосовать: не нравится

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]}?

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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.
  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Does this AC though?

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      32 часа назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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 :( )

»
3 дня назад, # |
  Проголосовать: нравится -20 Проголосовать: не нравится

Wtf segment tree problem in D? It is bad

»
3 дня назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится +6 Проголосовать: не нравится

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

  • »
    »
    3 дня назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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?

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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?

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится +29 Проголосовать: не нравится

    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$$$.

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится -8 Проголосовать: не нравится

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.

»
3 дня назад, # |
  Проголосовать: нравится -14 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится +11 Проголосовать: не нравится

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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится -8 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Someone explain C like I am 8 years old.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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?

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится +12 Проголосовать: не нравится

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

  • »
    »
    2 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      2 дня назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      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.

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится +43 Проголосовать: не нравится

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

»
3 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится -10 Проголосовать: не нравится

    because python is stupid language

    • »
      »
      »
      2 дня назад, # ^ |
      Rev. 4   Проголосовать: нравится +4 Проголосовать: не нравится

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

  • »
    »
    2 дня назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    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.

  • »
    »
    16 часов назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    knowing this has changed my life.

»
3 дня назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

»
2 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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?

»
2 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
2 дня назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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

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

dope

»
2 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 :(.

»
2 дня назад, # |
  Проголосовать: нравится -14 Проголосовать: не нравится

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

»
2 дня назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

My submission

»
41 час назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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.

  • »
    »
    37 часов назад, # ^ |
      Проголосовать: нравится +7 Проголосовать: не нравится

    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.

»
38 часов назад, # |
  Проголосовать: нравится -15 Проголосовать: не нравится

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
  • »
    »
    37 часов назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.

»
18 часов назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

why this code 299978985 is getting accepted but this one 299979242 is giving TLE although both are same. Thanks in advance.

»
17 часов назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I did an iterative seg tree version for D if smn wanted it. It was a bit tricky.

sub 300006513

»
16 часов назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

why does the python OrderedDict gives TLE for B?