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

Автор decoder__, история, 4 года назад, По-английски

Can anyone help me why my code is giving runtime error on cf but works fine on my local machine? Code: https://codeforces.me/contest/1215/submission/89127974 Problem: https://codeforces.me/contest/1215/problem/C Will codeforces compiler give an error if a vector is empty and size function is called on the vector ?

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

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

for (ll i = 0; i < inda.size() - 1; i += 2) {

vector.size() is unsigned type, for empty vector this loop runs until using i as an index causes runtime error.

Better use for (ll i = 0; i+1 < inda.size(); i += 2) {

I assume that testcase would not work on you machine, too.

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

    Can you elaborate on this?

    How thisfor (ll i = 0; i+1 < inda.size(); i += 2) { will work but not this for (ll i = 0; i < inda.size() - 1; i += 2) {

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

      size() returns an unsigned integer. Unsigned integers can't be negative. If you do an operation on an unsigned integer that results in a negative value, it wraps around to a positive value. Also, when you do an operation involving signed and unsigned integers, the signed operand is converted to an unsigned integer, resulting in negative values wrapping around.