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








Auto comment: topic has been updated by CS_alpha (previous revision, new revision, compare).
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://mirror.codeforces.com/blog/entry/72525Get it.
Maybe you help me debug and solve a problem in future contests.
Thanks. Now I fixed it.
~~~~~
sort(all(aa),[&bs,&n](int u, int v) {
if(u<v){
if(bs[(u-1)*n+v-1]) return true;
else return false;
}else{
if(bs[(u-1)*n+v-1]) return false;
else return true;
}
});
~~~~~