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

×

INTEST : Time Limit Exceeded in C++

I am trying to test my I/O methods via problem: INTEST http://www.codechef.com/problems/INTEST. On submitting it says time-limit exceeded. I previously submitted the similar problem in C, which gets accepted and takes 1.3 sec but for C++, its going beyond 8 sec. Is there any serious bug in my code or my I/O method is really so bad. Please help guys. As I am trying to switch from C to C++, its really demoralizing. Here is my source code:

#include <iostream>
using namespace std;

int main(){
  int test, div;
  cin >> test >> div;
  int count = 0;
  int num;
  while(test--){
    cin >> num;
    count += (num%div ? 0 : 1);  
  }
  cout << count << endl;
  return 0;
}

asked 19 Dec '13, 21:46

ks2bmallik's gravatar image

2★ks2bmallik
854814
accept rate: 22%


http://discuss.codechef.com/questions/32104/intest-time-limit-exceeded-how-to-shorten-time-duration-taken-to-run-this-program?page=1#32122

cout/cin simply is too slow, check the above link. You can either use scanf/printf for much faster I/O or as @kuruma explains - turn off auto sync with stdio and then using cout/cin will give you the time advantage.

Cheers

link

answered 19 Dec '13, 21:58

yashkumar18's gravatar image

5★yashkumar18
82661224
accept rate: 18%

hey i think as cin is tied with cout it will be slow

you can view my accepted code

http://www.codechef.com/viewsolution/3113193

here is link for more help

http://codeforces.com/blog/entry/5217

link

answered 19 Dec '13, 22:16

nitish1402's gravatar image

3★nitish1402
31115
accept rate: 0%

thanx guys..:D

link

answered 20 Dec '13, 21:36

ks2bmallik's gravatar image

2★ks2bmallik
854814
accept rate: 22%

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:

×1,917
×266
×75
×49
×49
×38

question asked: 19 Dec '13, 21:46

question was seen: 4,332 times

last updated: 20 Dec '13, 21:36