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