CENS20G - Editorial

I guess i did it right but got TLE please explain
https://www.codechef.com/viewsolution/37005035

When i coded in python i got tle but when i did in cpp i got AC? Why did this happen?

Tried that too…still same result.

Yup that was the typo problem, also i would like to mention that there is no need to initialise map elements to 0 explicitly, they are by default set to 0

What about my code. Is there anything im missing? CodeChef: Practical coding for everyone

totally waste question…i just use endl in place of \n and its showing tle.…i wasted 1 hour on that…such a waste question…

If my reply dosen’t work for you dosen’t mean that it won’t work of anyone. See the replies before you type “F”.

That ‘F’ is for the question…

Hello everyojne
my code is still giving tle
i have one every possible thing to optimize pls help
#include
#include<bits/stdc++.h>

typedef long long ll;
using namespace std;


int main()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);

    ll t;
    cin>>t;
    while(t--){
        string s;
        ll x1,y1,x2,y2,Q;
        cin>>s;
        cin>>x1>>y1;
        cin>>Q;
        while(Q--){
            cin>>x2>>y2;
            
            map<char,ll> m;
            m['U']=0;
            m['R']=0;
            m['D']=0;
            m['L']=0;
            for(ll i=0;i<s.length();i++){
                m[s[i]]++;
            }
            ll h,v;
            
            bool hp=true;
            h=x2-x1;
            v=y2-y1;
            if(h>0){
                if(m['R'] < h)
                    hp=false;
            }else if(h<0){
               if(m['L'] < abs(h))
                    hp=false;
            }
        
            bool vp=true;
            if(v>0){
                if(m['U'] < v)
                    vp=false;
            }else if(v<0){
               if(m['D'] < abs(v))
                    vp=false;
            }
        
            if(hp&&vp)
                cout <<"YES " << abs(h) + abs(v) <<"\n";
            else
                cout<<"NO"<<"\n";
        }

    }

    return 0;
}

**Edit **
Got it: i was again an again reinitializing map m for every query
map need to initialized only once in each test case

not waste completely!! That one hour taught you not to ignore such minor things written on question. It also increases your understanding of c++ that “endl” and “\n” are not same :slight_smile:

3 Likes

I had tried submitting in Python using sys library and “\n” and that was not working either. It worked on C++ with “\n” instead of endl.

that is not possible I have submitted the code and it passed all the test case look the code which i provided its yours only!!!

    Caught this Block
ll R=0,L=0,U=0,D=0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='R')
            R++;
            
            else if(s[i]=='L')
            L++;
            
            else if(s[i]=='U')
            U++;
            
            else if(s[i]=='D')
            D++;
        }

Bro you are recalculating L,U,D,R for every query in a test case ,
whereas these variable are fixed in each test case
You need to calculate them only once in each test case

if still unclear

position the above block before while(q–){ } block

@ajaysr Use “\n” instead of endl. “\n” is faster. I submitted your same code with “\n” and got AC CodeChef: Practical coding for everyone

think this if xdiff=0
then no matter what still else block in this code is going to execute (which was expected for xdif<0)

		if(xdif>0){
			if(xdif<=countR){
				flag1=true;
				ans1=(xdif);
			}
			
		}
		else{
			if(abs(xdif)<=countL){
				flag1=true;
				ans1=(abs(xdif));
			}
		}

Can ayone explain how the time complexity is calculated here??

try to use \n rather than endl.

because of endl i lost my rating now i will always use “\n”

Hello everyone,

I have used fast io as suggested but still I am getting TLE. Can anyone let me know the reason?

Thanks in Advance!!

I thought it diffrently as it was a reply ,lol. Anyways peace .