Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

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

Автор vinhhocptit, история, 5 часов назад, По-английски

Today, I had spent hours trying to solve problem : (https://codeforces.me/contest/1334/problem/C) and submitted several solutions with an O(n) complexity, but I kept getting a TLE. After some further inspection, I changed from using cin to scanf, and my solution was finally accepted. While it's well-known that scanf is slightly faster than cin, I was surprised by how much of a difference it actually made in this case.

TLE Code : 284211972 Accepted Code : 284212724

This blog may not be overly educational, but I just want to highlight that even a O(n) solution can lead to a TLE error. Understanding this can help you avoid the frustration and wasted time that I experienced.

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

»
5 часов назад, # |
Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

ios::sync_with_stdio(false);cin.tie(nullptr); cout.tie(nullptr);

Use this if you want to use cin/cout

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

    what does cout.tie(nullptr) do?

    explanation

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

      cout = Output + cout.flush(). But if you use cout.tie(nullptr) it will untie the cin with cout. So cout = Output only. In interective problems if you don't use cout.tie(nullptr); you don't have to use flush manually as it will be done automatically.

      Using this will help a lot with time complexity specially when you have multiple test cases.

      And forgive my poor English

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

        no, it's all about cin.tie(nullptr); as far as i know

»
5 часов назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

I thought everyone learns to put cin.tie(0) -> sync_with_stdio(0) in their code the first day they start cp.

Guess not :/

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

    I know thats a fundamental thing in CP but I never thought that it would matters that much but I guess im wrong lol.