I was working on this problem: https://codeforces.net/problemset/problem/547/B
My first submission is: https://codeforces.net/problemset/submission/547/303635124
This code resulted runtime error on test 49
However, when I removed the equal sign in my comparator function:
bool cmp(int x, int y){ return a[x] <= a[y]; }
My new submision is: https://codeforces.net/problemset/submission/547/303635252
The code was accepted! Can anyone explain why this change made a difference? Thanks!
I'm pretty sure that sort function needs a strict ordering on the elements.
If you just run this code
There are some incorrect values at the beginning of the array sometimes, looks like the implementation of the sorting function relies on the ordering being strict, otherwise it can be undefined. I stumbled upon this some time ago when using a custom comparison on a set. I think the way they establish whether two elements are equal is that !(a < b) && !(b < a) => a = b. This wouldn't work for your comparison — there could be something similar in sorting function.
The return value of the function call operation applied to an object of a type satisfying Compare, when converted to bool, yields true if the first argument of the call appears before the second in the strict weak ordering relation induced by this type, and false otherwise.
Took this from cppreference. You can read the full Compare requirement here.
In C++, comparator should return false if its arguments are equal