hello everyone
i'm trying to sort a vector using an explicit compare function
why does this simple code gives me Runtime Error sometimes and sometime Time Limit Exceeded
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
hello everyone
i'm trying to sort a vector using an explicit compare function
why does this simple code gives me Runtime Error sometimes and sometime Time Limit Exceeded
Name |
---|
Short answer: You should change:
to
can you give me the long answer please ?
Here are the requirements for a Compare function: cppreference Basically the compare function needs to define a strict weak ordering. If your compare function returns true, you're telling the sort function that the first value has to appear before the second element. So if
cost(f) == cost(s)
, then the program thinks that the valuef
has to appear befores
, and alsos
has to appear beforef
(becausecost(s) == cost(f)
). So it doesn't know what to do and gives a runtime error.Maybe this is helpful?
EDIT: Sniped
Jakube
dpaleka
thank you very much
I have just tried to compile and run your code using GNU C++17 Custom Invocation in CF
The following is the program output:
Done
=====
Used: 2839 ms, 3660 KB
The following is the program output after replacing the
<=
operator with<
operator in thecomp
function.Done
=====
Used: 15 ms, 3680 KB
i really don't know why this worked ,, sorry