vagi's blog

By vagi, history, 4 years ago, In English

This is about yesterday's Div2 contest Problem B. For reference, here's my code which got RE on system test 7. Finally, I fixed my code and got AC. Read the blunder I made. I hope you people handle it elegantly the next time you encounter this. Pay attention to the last part of the code which includes the two for loops.

Wrong code

The problem here is while outputting the answer, I write the loop's end condition as i < o.size()-1 and i < e.size()-1 . Now, size() returns an unsigned int (In this case, an unsigned 0) and when I subtract 1 from it, like this i < e.size()-1, I get a huge number instead of -1 which I expected I'd get. The loop runs for quite a long time instead of never running at all.

Now, to fix this there are 3 ways you can do so.

  1. Typecast the returned value into int like this (int)(e.size()-1).
  2. Store this returned value from size method into a variable like this int sz = e.size().
  3. As HideBehind mentioned, the simplest way is to write it as i+1 < e.size().

Here's my working code showing both the methods.

Got AC with it

I felt like a dumbo after RE on system tests, fixed it, and then thought to inform you all guys (my first blog).

PS: Making a mistake like this deals some serious damage on ratings XD

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

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by vagi (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Interesting

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

That's why you should use $$$ i + 1 < e.size() $$$ instead of $$$ i < e.size() - 1 $$$

By doing that you can avoid problems with unsigned numbers described above.

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

    Updating the topic. A better way indeed!

»
4 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Even if not empty it returns an unsigned int.

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

    Yes, I mentioned that on being empty it returns an unsigned 0. No doubt, it returns an unsigned int always.

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Yeah, as other people has mentioned it always returns an unsigned value. The only reason it works in the other case is because unsigned 1-1 is still nonnegative. There is nothing special about the return type when it is empty.

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

    Yeah, I framed the sentence incorrectly. Maybe I shoud rephrase it.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by vagi (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

My solution is also a little bit same. Did I do it correct? https://mirror.codeforces.com/contest/1370/submission/84467854 It passed the system tests but the method is similar and i didn't typecast anything.

»
4 years ago, # |
  Vote: I like it +6 Vote: I do not like it

You can compile your source with the flag -Wall to issue warnings from the compiler when such comparisons (signed / unsigned) are made.

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

    I tried it and got a warning. Thanks for this :)