I submitted a solution for the problem "http://codeforces.me/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.me/contest/14/submission/11125428 (without '#define's)
(All other submissions) http://codeforces.me/contest/14/submission/11123710 http://codeforces.me/contest/14/submission/11123757 http://codeforces.me/contest/14/submission/11123811 http://codeforces.me/contest/14/submission/11123825 http://codeforces.me/contest/14/submission/11124124 http://codeforces.me/contest/14/submission/11125233
Thanks in advance
size()
method returns an unsigned integer, so ifles
is empty thenles.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 Codeforcessizeof(size_t) = 4, sizeof(long long int) = 8
.That was very useful. Thanks for helping.
I can see how thankful you are from your profile pic :P
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
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.
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
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 .