During these several days, I have suffered from runtime errors while solving Baekjoon Online Judge problems.
Of course, I managed to solve them but I think it is useful to wrap up the reasons why I got runtime errors this time.
These are general cases that cause a runtime error.
-
The code refers to the spot out of the range of given arrays.
-
The size of a global array exceeds the size of the memory’s data section.
-
The size of a local array exceeds the size of the stack.
-
A number is divided by $0$.
-
The library itself triggers an exception.
-
Recursive calls become too deep.
-
The code refers to the already released memory section.
-
The main function returns a number which is not $0$.
(Reference: https://www.acmicpc.net/board/view/22980)
I made two mistakes.
One case is that I misunderstood the problem so I declared the size of a vector smaller than that of the problem’s condition and the spot which is located out of the array was referred to.
And another case is that when I built a customized comparing function to use sort
, I used <=
and >=
, not <
or >
.
The former is one of the cases I mentioned, so I will skip it.
Let’s focus on the latter.
https://en.wikipedia.org/wiki/Weak_ordering
This is the reference about the above error and honestly, I have no idea what this document is saying. For a simpler version, this is what I understood.
When sorting the values, the standard usually has a condition like this: As $a<b$, therefore “a” should come before “b”.
In other words, the statement that if $a<b$, this must not be $b<=a$ should be guaranteed to implement a proper sorting mechanism
. If one wants to include the “same” case and puts a sign of equality to inequality signs, the compiler understands the code like this: Both $a<b$ and $b<a$ are correct.
This is obviously a contradiction, so it triggers a runtime error.
Actually, I have seen that information somewhere but I just didn’t care about it that much.
Honestly, I didn’t expect to be blocked by this principle in problem-solving.
I’m learning a lot every day…