I used sort() in https://codeforces.me/contest/2056/submission/325199850 and got runtime error verdict, but used stable_sort() in https://codeforces.me/contest/2056/submission/325199048 and got accepted verdict.
Can someone please explain the reason? The rest of the code is unchanged.









Consider this test
your comparator:
comp(2, 1) = true
comp(1, 2) = true
Which order is correct —
1<2or2<1?std::stable_sortwill leave2<1(i thinkstable_sortwould change the order of two elements if bothcomp(x, y)andcomp(y, x)are true)shuffling before sorting would ruin your solution
both
comp(x, y)andcomp(y, x)being true is UB. https://codeforces.me/blog/entry/72525Get it.
Maybe you help me debug and solve a problem in future contests.