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

Автор XoXoHarsh, история, 19 часов назад, По-английски

I keep encountering a runtime error on test case 4 in my solution. I would greatly appreciate it if someone could help me identify the reason.

Problem Link: 1931E — Codeforces My Submission: 300919011

It might be a simple issue, but I’ve spent hours figuring it out without success.

  • Проголосовать: нравится
  • -8
  • Проголосовать: не нравится

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

Auto comment: topic has been updated by XoXoHarsh (previous revision, new revision, compare).

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

Just dont create unnecessary strings, and use long long everywhere.

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

    Thank you for providing the corrected code. However, I am still confused about why using long long or a string everywhere would result in a runtime error.

    Is it related to the internal workings of the sort function? I don’t think I have used string or long long in a way that would cause an MLE (Memory Limit Exceeded) or runtime error.

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

      yes, I try to avoid to_string() just because it can cause memory to get filled very fast. Also internally when you are sorting, >= sign can change the relative postions of the elements. rather use stable_sort() if you want to use >=.

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

When using a comparison function in sorting, it is incorrect to use: cnt1 <= cnt2

The correct approach is: cnt1 < cnt2

This ensures that the sorting algorithm properly defines a strict ordering.

Your code you can check line 138

If you return true when the arguments are equal, it implies that you want the first element to precede the second, while simultaneously wanting the second element to precede the first. This creates a contradiction.

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

    Oh, I didn’t know about that. Thanks! Just removing the ‘=’ made the code work. But why is strict ordering necessary?

    I mean, doesn’t the comparison function only need to return true or false? Why does strict ordering matter?

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

Auto comment: topic has been updated by XoXoHarsh (previous revision, new revision, compare).