Блог пользователя salt_n_ice

Автор salt_n_ice, 3 года назад, По-английски

link

My solution

As the constraint is m*n<=10^6, this solution should work under 1 second but it's taking about 1871 ms in one of the TLE'd test case. Why could this be happening?

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

add after declaring v.reserve(n*m);

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

It is RTE, try "resize" instead of "reserve".

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I tried both, still it takes around 1880 ms..

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +9 Проголосовать: не нравится

      Now got it. Don't use "endl", use "\n" instead.

      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Works now. Thanks!

      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        using "\n" instead makes that much of a difference ??

        • »
          »
          »
          »
          »
          3 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          endl is write a newline and flush the stream. flush the stream each line take lot of time.

        • »
          »
          »
          »
          »
          3 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          From the Competitive Programmer's Handbook, which I'm reading now: "Note that the newline "\n" works faster than endl, because endl always causes a flush operation."

          If there are many lines in the output, then all these flushes may kill performance.

          • »
            »
            »
            »
            »
            »
            3 года назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            i see.. but my hands are set on endl.. we can always #define endl "\n" right ?

            • »
              »
              »
              »
              »
              »
              »
              3 года назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

              Until you face an interactive problem -> where you would need endl to flush the stream

              • »
                »
                »
                »
                »
                »
                »
                »
                3 года назад, # ^ |
                  Проголосовать: нравится +3 Проголосовать: не нравится

                For interactive problems, '\n' will also work because a read from cin forces to flush cout, unless cin.tie(0) is used:

                cin is tied to the standard output stream cout (see ios::tie), which indicates that cout's buffer is flushed (see ostream::flush) before each i/o operation performed on cin.

                For non-interactive problems cin.tie(0) should be used, to prevent the flush operations, in case there are some interleaving input and output operations.