I was working on this problem: https://mirror.codeforces.com/problemset/problem/547/B
My first submission is: https://mirror.codeforces.com/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://mirror.codeforces.com/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.
In C++, comparator should return false if its arguments are equal