a7t's blog

By a7t, history, 7 years ago, In English

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

  • Vote: I like it
  • -5
  • Vote: I do not like it

»
7 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

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.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.