oqfiqh's blog

By oqfiqh, history, 11 days ago, In English

I solved problem B (permutation chain) of the last contest, and the same exact program using printf and scanf (stdio.h) went over the time limit by a fair chunk (exceeded the time limit by 2s for a single test),instead when using cout and cin (iostream) it passed all tests in 100ms.

Does anyone have any idea why this is happening?

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
11 days ago, # |
  Vote: I like it 0 Vote: I do not like it
  • »
    »
    10 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks for the resources. What I find odd is that my program with scanf and printf was slower ( and by a substantial amount) than the one with cout and cin, when It should have been the opposite.

    • »
      »
      »
      10 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sorry, I misunderstood your question. Can you share the source codes? Because I never faced anything like that before.

      • »
        »
        »
        »
        10 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        This is the one with printf and scanf, and this the one with cout and cin. The only thing that changes between them is literally only the functions of input and output. Nevertheless, the first one exceeds test 3 with a mammoth 2 seconds. The second one only needs 140ms.

        • »
          »
          »
          »
          »
          10 days ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I submitted your previous code in C++17 and it got accepted. Link here

          • »
            »
            »
            »
            »
            »
            10 days ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Seems to be something buggy with the site. Will submit with C++17 from now on. Thanks for the help.

    • »
      »
      »
      10 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Also, can you mention the language you selected while submitting. I saw in some places, people are getting tle while submitting in GNU C11.

      • »
        »
        »
        »
        10 days ago, # ^ |
        Rev. 3   Vote: I like it 0 Vote: I do not like it

        I submitted with GNU C11 because I was using C, then since it didnt work I "translated" to C++ and submitted with C++ 17 . But program are identical, it literally changes the library included and the i/o functions.

        Edit: As confirmed by others, the problem was indeed the language selected for submitting.

»
10 days ago, # |
  Vote: I like it +3 Vote: I do not like it

It's not actually anything to do with scanf / printf (if you submit your scanf / printf code as c++ it works fine — https://codeforces.com/contest/1716/submission/167324397).

I've no idea why c is running so much slower, but someone else was reporting the same thing (https://codeforces.com/blog/entry/105611).

  • »
    »
    10 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Ahh thanks, this thing bugged me so much, i couldnt understand what was going on.

»
10 days ago, # |
  Vote: I like it +22 Vote: I do not like it

The reason is similar to the one mentioned in this blog.

In the blog it is mentioned that the patch was applied for the MinGW implementations for C++, but I am not sure if it was applied for C or not. Or perhaps they were thorough in applying their patches and Codeforces didn't update the version of MinGW they use for C.

Anyway, there is a simple way to fix this: add this line to the top of your submission, and it will revert to the faster implementation (at least as of now on Codeforces): #define __USE_MINGW_ANSI_STDIO 0. For example, 167339388 which has your code with this one extra line at the top gets AC.

A word of caution though: don't rely on this behaviour on other systems, since it is an internal implementation macro and the behaviour is not guaranteed to be the same everywhere (and there were some discussions on deprecating this, not sure if it was deprecated or not).