PROBLEM CODE: CEILSUM Problem - CodeChef
My code:
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin>>t;
while(t–) {
long long int a,b,m=INT_MIN;
cin>>a>>b;
long long int p = min(a,b);
long long int n = max(a,b);
long long int r1,r2,r;
for(long long int i=p;i<=n;++i) {
r1 = ceil(((b-i)/2.0));
r2 = ceil(((i-a)/2.0));
r = r1+r2;
if(r>m) {
m = r;
r=0;
}
}
cout<<m<<endl;
}
return 0;
}
Though this is working for sample test case but showing TLE. Please help.
Given constraints are 1 to 10^9. O(N) would not work for anything greater than 10^8. Others can correct me if I’m wrong.
so is there any way to correct this ?
I don’t think so. I suggest you think of a different approach.
There is a way to solve each test case using just one operation. Think for a while by taking some sample cases. There is just a bit of casework involved. If you can figure it out then great otherwise the editorial has already been posted.
1 Like
You need not check for all the numbers right,
check for all possible cases i.e A>B, A<B, A=B
And find how would you consider taking the k value for each case!