×

# TLE problem

 0 Can anyone pls tell which is the best way to avoid tle in cpp...i am tired of searching tags...thanks asked 07 Dec '13, 17:11 1★chaseme 626●9●24●43 accept rate: 0%

 0 Algorithm ( This is the first thing you have to take care about ) Apart from that Use scanf and printf instead of cin and cout , the latter is 3 times slower than the scanf nd printf Try to precalculate most of the things that remains constant through out the code answered 07 Dec '13, 19:38 1.2k●2●11●16 accept rate: 10%
 1 @chaseme.... Main reason of coming TLE is that your algorithm is not efficient .It does not depend on any programming language.If you want to avoid TLE then apply an algorithm with lesser time complexity answered 07 Dec '13, 18:14 2★raj01104 245●2●4●10 accept rate: 0% my algorithm is maximum optimised...i only need to know how to read input and output fast... (07 Dec '13, 18:46) chaseme1★
 1 The first and foremost thing is to get a good algorithm. If your algorithm is correct you have 90% of your work done. How you get to it depends on you.Now once you have an algorithm here are some simple tips regarding implementation that will help speed up your code: 1) I/O Methods: The easiest optimization is changing your input/output method. Using fast input methods like use of getchar_unlocked() can speed up your program by 10-20 times in programs with very large input/output limits. You can read more about fast I/O here 2) Iterate : Recursion normally involves a large number of function calls. At each step the system may have to push and pop a large amount of data from the system stack which has a lot of overhead. Try to use iterative solutions wherever possible.However they are normally more difficult to think of than recursive solutions. 3)MACROS: Using functions again involves the pushing and popping of large data on the system stack as execution will have to jump from one segment of memory to another. However when you use macros, the macro call is directly replaced by the full code defined for macro. Hence the execution continues in the same block of memory and there is no need of a jump or system stack. This reduces the overhead and helps speed up your code. Good luck! answered 07 Dec '13, 20:50 3★kcahdog 10.0k●28●54●129 accept rate: 14%
 0 Use a better algorithm. answered 07 Dec '13, 18:13 3.4k●19●43●77 accept rate: 16%
 0 You can follow the method that infinitum stated. But if you insist on using cin/cout. Add this line just after your mainios_base::sync_with_stdio(0); What this does is it terminates the synch between the standard IO streams. You can read more about it here. answered 07 Dec '13, 20:47 1★vigonoid 168●2●6●14 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×720
×94

question asked: 07 Dec '13, 17:11

question was seen: 2,262 times

last updated: 07 Dec '13, 20:50