dush1729's blog

By dush1729, history, 9 years ago, In English

How to take fast I/O in C++? I am solving a problem where number of test cases can be 10^6 so i guess fast I/O will help to speed up my solution.

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

| Write comment?
»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it
  • »
    »
    9 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    +1 but sometimes it's not enough

    Another possible way is to implement your own reader for integer/real/signed types using getchar():

    int next_int() {
      char c;
      do { c = getchar(); } while( c != '-' && !isdigit(c) );
      bool neg = (c == '-');
      int result = neg ? 0 : c - '0';
      while( isdigit(c = getchar()) )
        result = 10 * result + (c - '0');
      return neg ? -result : result;
    }
    

    In most cases coders use it to get top1 by execution time))

    But in case N = 10^6 it may be useful to beat TL with solution that wasn't intended by jury.

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

      Thanks it did reduce my time but still TLE. I guess i have to improve my algo.