NM_Mehedy's blog

By NM_Mehedy, history, 3 years ago, In English

I solved this problem in CF #705. Screenshot-450

But it failed system testing showing Screenshot-451

TLE code

Accepted code after contest

If we compare this two submissions-

Screenshot-452

This is the only difference.

Definition of see(...) in my code

The main thing is I used cerr for debugging and it got TLE.

Can anyone explain Why did it happen?

  • Vote: I like it
  • -19
  • Vote: I do not like it

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

Great

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

Nothing is absolutely free. Your debugging print code consumes precious CPU cycles.

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

    If I recall correctly, on some online judges cerr is sent to /dev/null, and "writing" there is very fast. Sadly, not on Codeforces.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +14 Vote: I do not like it

      Even if cerr is redirected to /dev/null, the useless output strings are still being created in huge amounts. The blog author's main loop contains only a tiny amount of arithmetic operations. But each of these loop iterations also tries to at least create one useless std::string and print it to cerr with a ton of overhead.

      The failing testcase prints 211.5k lines to stderr and 100 lines to stdout. But I agree that this shouldn't have been fatal. On my computer it takes just slightly more than one second to run if I redirect stderr to /dev/null. And my computer is slower than the codeforces judge.

      It's safer to put the debugging print code inside of an #ifndef ONLINE_JUDGE block either way.

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

    This is why some people define a macro with a local flag instead of a function for debugging: if the local flag is not defined, then the macro will become nothing, and nothing will be run.

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

Very evil. Accepted: 117533891. Idleness limit exceeded: 117532620. It's not even an interactive problem.

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