desikachariar's blog

By desikachariar, 9 years ago, In English

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

  • Vote: I like it
  • +1
  • Vote: I do not like it

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

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 years ago, # |
  Vote: I like it +1 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    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 .