You are not logged in. Please login at www.codechef.com to post your questions!

×

TLE problem

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

chaseme's gravatar image

1★chaseme
62692443
accept rate: 0%


Algorithm ( This is the first thing you have to take care about )

Apart from that

  1. Use scanf and printf instead of cin and cout , the latter is 3 times slower than the scanf nd printf
  2. Try to precalculate most of the things that remains constant through out the code
link

answered 07 Dec '13, 19:38

infinitum's gravatar image

3★infinitum
1.2k21116
accept rate: 10%

@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

link

answered 07 Dec '13, 18:14

raj01104's gravatar image

2★raj01104
2452410
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★

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!

link

answered 07 Dec '13, 20:50

kcahdog's gravatar image

3★kcahdog
10.0k2854129
accept rate: 14%

Use a better algorithm.

link

answered 07 Dec '13, 18:13

wittyceaser's gravatar image

2★wittyceaser
3.4k194377
accept rate: 16%

You can follow the method that infinitum stated. But if you insist on using cin/cout. Add this line just after your main
ios_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.

link

answered 07 Dec '13, 20:47

vigonoid's gravatar image

1★vigonoid
1682614
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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