Problem Link: CodeChef: Practical coding for everyone
I have defined a macro file_i_o as “ ios_base:sync_with_stdio(0);cin.tie(0);cout.tie(0); “
Solution:
file_i_o;
int tc;
cin>>tc;
while(tc-- > 0){
int N,M;
cin >> N >> M;
int a[N+1];
a[0] = -1;
for(int i = 1; i <= N; ++i)
cin>>a[i];
int left[N+1], right[N+1];
int prev = INT_MAX;
left[0] = right[0] = INT_MAX;
for(int i=1;i<=N;++i){
left[i] = prev;
if(a[i] == 1){
prev = i;
}
}
prev = INT_MAX;
for(int i=N;i>0;--i){
right[i] = prev;
if(a[i] == 2){
prev = i;
}
}
int s = M;
while(s--){
ll q;
cin>>q;
if(a[q] || q==1){
cout << 0 << spc;
}else if(left[q] == INT_MAX and right[q] == INT_MAX){
cout << -1 << spc;
}else{
cout << min(abs(q-left[q]),abs(right[q]-q) ) << spc;
}
}
cout<<endl;
}
If i don’t include those lines defined by macro in my code, then i am getting a TLE.
And if i include those three lines then it is getting submitted in 0.16 seconds(AC).
Why is this difference so big?