DmitryS's blog

By DmitryS, history, 4 weeks ago, In English

Hi guys!

Solving the 1574C problem ended with TIME_LIMIT_EXCEEDED. Comparing my code with the Solution I found only 2 significant differencies: ios_base::sync_with_stdio(false); cin.tie(NULL);

Just compare 130080268 with 130164765.

Adding this lines to my code I had Accepted solution. So I am surprised that C++ I/O operations without optimizations is so computationally intensive. Is there any other tips to improve the code? May be standard C I/O should be used?

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

»
4 weeks ago, # |
Rev. 7   Vote: I like it 0 Vote: I do not like it

Check the execution time after replacing

    cout << min_gold << endl;

with

    cout << min_gold << '\n';

It is well-known that the std::endl manipulator flushes the standard output stream buffer after putting the new-line character. Frequent flushing of the output stream buffer can lead to TLE Verdict in competitive programming contests, and is usually needed in interactive problems only.

I often use the tie/sync_with_stdio calls in a single statement as follows.

cin.tie(nullptr)->sync_with_stdio(false)

In very few problems, you may even need faster data I/O in C++ program. Check the following blog and its comments for more details.

G++11/17 input/output speed?