In the recently concluded Codechef September Lunchtime, I faced a strange behaviour in the Div1 3rd problem. When I wrote the comparator for sorting based on inversions in this way I got SIGSEGV verdict on almost all testcases:
bool cmpx(int A, int B) { if((zz[A] * (sze[B] - zz[B])) < (zz[B] * (sze[A] - zz[A]))) return 0; else return 1; }
But, when I just reversed the condition for A and B in this, all testcases passed:
bool cmpx(int A, int B) { if((zz[B] * (sze[A] - zz[A])) < (zz[A] * (sze[B] - zz[B]))) return 1; else return 0; }
Can anyone explain this strange behaviour as I'm unable to understand this?
Problem Link: https://www.codechef.com/problems/TREEVERS
SIGSEGV Solution: https://www.codechef.com/viewsolution/26846332
AC Solution: https://www.codechef.com/viewsolution/26847767
HOW. MANY. TIMES. CAN. WE. REPEAT. IT?
Comparator should return false on equal elements.
Repeating (povtorenie) is a mother (mat') of learning (uchenia).
OK!! Never faced something like this before. Sorry for the inconvenience caused.
https://stackoverflow.com/questions/45929474/why-must-stdsort-compare-function-return-false-when-arguments-are-equal
Reason could be found here!