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

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

Hi there! Just finished another great contest, hope you enjoyed it too!

Nevertheless, I run into this strange problem, my submission on Codeforces says that output is '3', but on another IDE (codechef, cpp.sh or ideone.com) it says '2', which is the correct answer. I also have tried different versions of compiler, however result is the same. Do you know why this happened?

Link to my code on ideone.com — https://ideone.com/KZ76Aq

My submission — 39627475

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

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

Check the minSwap() function, there's an illegal memory access which is undefined behaviour. You're likely trying to read from outside the vector.

In the future, try to use use proper compiler flags Catching silly mistakes with GCC to catch this kind of errors while testing locally.

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

    Ok, I see. But how does it affect on submissions on totally different IDEs?

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

      Since there is undefined behaviour, the output of the code can be anything — it's not well defined. Even if the program gives a correct answer on some IDE, it's just luck. It's possible that the output changes if you run the same code multiple times.

      Generally: "My code outputted x locally and y in Codeforces" = undefined behaviour.