INTEST : Time Limit Exceeded in C++

c-plus-plus
exceeded
execution-time
intest
time-limit
timelimit

#1

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;
}

#2

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


#3

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


#4

thanx guys…:smiley: