div24ever's blog

By div24ever, history, 4 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

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it
  • »
    »
    4 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.

    • »
      »
      »
      4 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.

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

I got TLE in Educational Round 1 D because of cin/cout :(

After contest I submitted with printf and got AC.

I should have used cin tie.

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

fread + buffer could be very fast , take a look at my code (i used it in spoj many times) http://ideone.com/461DPt