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

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

I submitted a solution for the problem "http://codeforces.com/problemset/problem/14/B" Though the solution has a logical bug, It ran perfectly in my system (gave a output) but when submitted here it gave a runtime error. This happens sometimes with my friend also here. Can anyone help me with how this happened.

Submissions: http://codeforces.com/contest/14/submission/11125428 (without '#define's)

(All other submissions) http://codeforces.com/contest/14/submission/11123710 http://codeforces.com/contest/14/submission/11123757 http://codeforces.com/contest/14/submission/11123811 http://codeforces.com/contest/14/submission/11123825 http://codeforces.com/contest/14/submission/11124124 http://codeforces.com/contest/14/submission/11125233

Thanks in advance

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

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

size() method returns an unsigned integer, so if les is empty then les.size()-1 overflows and becomes a large positive value.

Upd: the difference between your local run and Codeforces is probably the sizes of integer types. On my system sizeof(size_t) = sizeof(long long int) = 8, on Codeforces sizeof(size_t) = 4, sizeof(long long int) = 8.

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

ill countl = les.size()-1
vector's size can be 0, and it is unsigned int. So count1 will be very big number.

Just write:
ill count1 = int(les.size())-1

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

    But then It should overflow in my system also right. Also ill is not unsigned but signed only. Anyway thanks for replying. I got where my mistake could be. I'll avoid writing like this next time.

    Thanks again.

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

while(countl >= 0 and les[countl] >= i)

The problem lies in this line If count is -1 or less than 0 First condition is false but it still evaluates the second condition and returns false

So in that way you are trying to access count[-1]

To avoid this give the second condition inside the loop

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

    As far as I know, it doesn't evaluate the second condition if the first is false, I always use this and never got a runtime error because of it .