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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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