Блог пользователя justHusam

Автор justHusam, история, 9 лет назад, По-английски

Today I was solving this problem 632C - The Smallest String Concatenation.

The judge's response for this code 18114819 was Runtime error on test 8.

Then I changed only the last line in the sort comparison function (comp function) from (return true) to (return false) (code: 18114847), and the judge's response was Accepted.

Does anyone have an explanation for that?

Thank you.

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

Your comparator returns 1 for comp(a, a) which is incorrect.

»
9 лет назад, # |
  Проголосовать: нравится -11 Проголосовать: не нравится

1800+ guy asking this... Well, I have an explanation in just four letters!

Spoiler
  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится +43 Проголосовать: не нравится

    Thank you for your high morals.

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится +17 Проголосовать: не нравится

    LOL!! You're participating for more than 5 years and still violet!!

    Why so harsh? It's one of trickier technical details.

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится -15 Проголосовать: не нравится

      I'm violet because I'm not able to solve hard problems. Everyone has their limits in different areas and it can't be fixed. From the other side, everyone is able to read a language manual. When I use a function, I read its docs. And you? The topicstarter doesn't. He didn't even try to find it in Google or Stackoverflow.

      • »
        »
        »
        »
        9 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I will surprise you but ability of understanding manuals and efficient googling is also a skill one can be good or bad at it. For example, I am at most cyan at both :). Even if it is possible that I will be able to find an answer in Google I often choose to ask somebody, because a friend will probably understand me better than Google.

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится +13 Проголосовать: не нравится

      Well. I have an explanation in just three letters!

      Spoiler
»
9 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Becuase std::sort is using operator < as a parameter, it is incorrect to use function which returns true in case of equality. This might sometimes end up in an RE. Not sure when exactly, but in most cases it will.

»
4 года назад, # |
  Проголосовать: нравится -13 Проголосовать: не нравится

The comparator function must meet the requirements of Compare concept i.e if comp(a,b) = true then comp(b,a) must return false and vice versa . Runtime error can occur in case where you have used <= or >= in your custom comparator function . hope you got it