XoXoHarsh's blog

By XoXoHarsh, history, 11 hours ago, In English

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.

  • Vote: I like it
  • 0
  • Vote: I do not like it

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

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

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

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

Your accepted code
  • »
    »
    9 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

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

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

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

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.

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

    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?

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

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