NFS - Editorial

my bad thk

Scanner scan = new Scanner(System.in);
int t=scan.nextInt();
while(t>0) {
int v=scan.nextInt();
int u=scan.nextInt();
int a=scan.nextInt();
int s=scan.nextInt();
if(v==u || Math.sqrt(((uu)-(2a*s)))<=v) {
System.out.println(“YES”);
}else {
System.out.println(“NO”);
}
t–;
}

I am getting TLE, pls see

#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main() {
int t;
cin>>t;
while (t–)
{
int u,v,a,s;
cin>>u>>v>>a>>s;
float finalspeed = sqrt(pow(u,2)-2as);
// cout<<finalspeed<<endl;

    if(finalspeed<=v){
        cout<<"Yes"<<endl;
    }
    else{
        cout<<"No"<<endl;
    }
}
   
return 0;

}
This is my code why I was getting wrong answer while submitting.

1 Like

int main()
{
int t;
cin>>t;
while(t–)
{
float u,v,a,s;
float ans=-1;
cin>>u>>v>>a>>s;
if(u<=v)
{
cout<<“Yes”<<endl;
}
else
{
ans=sqrt((uu)-(2a*s));
if(ans<=v)
{
cout<<“Yes”<<endl;
}
else
{
cout<<“No”<<endl;
}
}
}
}

Why am I getting a WA I don’t understand

@ninjacoder14
Use BufferReader for taking input…
Scanner class is bit slow wrt. BufferReader class.

for i in range(int(input())):
u,v,a,s = list(map(int, input().split()))
vf = ((u ** 2) - 2 * a * s)
vf_pow = vf ** 0.5
if (vf < 0 and u == v == a == s == 1):
print(“Yes”)
elif (vf < 0):
print(“No”)
elif (vf_pow > 0 and v >= vf_pow):
print(“Yes”)
elif (vf > 0 and vf_pow > v):
print(“No”)
else:
print(“No”)

What’s wrong in this code?

@abhicoder_03

Consider this case
1
1 100 20 20

Is the answer really ‘No’?

@witcomenight @polmbil
What happens if you take square root when u^2 - 2*a*s < 0? (This is discussed in the last part of the editorial, please read that)

@ninjacoder14
As kunal_srivast mentioned, Scanner is slow. Also, you have the same issue as above - think about when you can and cannot take square root.

#include <bits/stdc++.h>
#include<string.h>
using namespace std;
int main() {
	
	int t;
	cin>>t;
	while(t--)
	{
	    int u,v,A,s;
	    float v1;
	    cin>>u>>v>>A>>s;
	    if(v>=u){
	        cout<<"Yes"<<"\n";
	    }
	    else{
	        v1=sqrt(u*u-2*A*s);
	        if(v1<=(float)v){
	            cout<<"Yes"<<"\n";
	        }
	        else{
	            cout<<"No"<<"\n";
	        }
	    }
	}
	
	return 0;
}

my doubt is why is this wrong, i have checked all cases it should be correct. After competition when I tried this, it showed correct:-

#include <bits/stdc++.h>
#include<string.h>
using namespace std;
int main() {
	
	int t;
	cin>>t;
	while(t--)
	{
	    int u,v,A,s;
	    float v1;
	    cin>>u>>v>>A>>s;
	    if(v>=u){
	        cout<<"Yes"<<"\n";
	    }
	    else{
	        v1=sqrt(u*u-2*A*s);
	        if((int)v1<=v){
	            cout<<"Yes"<<"\n";
	        }
	        else{
	            cout<<"No"<<"\n";
	        }
	    }
	}
	
	return 0;
}

In the 2nd one, i type converted float to int and it gave correct answer, but in the first one type converted int to float, which should give more accurate result, and should be correct. Please tell me why is my 1st method wrong.

@iceknight1093 Sorry to ask again for the same Q.
But can you tell me what went wrong here. I was just fixing the runtime error. The output worked for given cases I think the problem lies with type declaration but I don’t know .

long long t;
cin>>t;
int u,a,s;
long long x,v;
while(t--){
	cin>>u>>v>>a>>s;
	x= sqrt((u*u)+(2*a*s));
	if(v+1>x){
		cout<<"YES"<<br;
	}else{
		cout<<"NO"<<br;
	}

}

Can you assist here ?

You can decelerate upto A m/s^2, or accelerate at -A m/s^2.
Your equation needs a minus sign, not plus. Change that and it passes.

1 Like

@iceknight1093 could you please clarify this query?

First, it’s not quite true to say that float is more ‘accurate’ than int - for example, it can’t even represent all the integers which int can without precision loss. However, that isn’t the problem here.

The main issue in your code is that you compute \sqrt{u^2 - 2*a*s} when the quantity inside the square root can be negative. float treats such a quantity as nan, whereas when you typecast it to int, it becomes INT_MIN which luckily works out for the comparison because if u^2 - 2*a*s < 0 the answer is always going to be true.

tl;dr whenever you compute the square root of something, make sure the that thing is not negative - you just got lucky here that C++ casts nan to INT_MIN when converted to int; and that might not always be what you want.

3 Likes

Can anybody please explain me why I am getting WA upon submission. And which case i am missing here

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
ll u,V;
cin>>u>>V;
ll a,s;
cin>>a>>s;

  if(u<=V)
  cout<<"Yes"<<"\n";
  
  else
  {
     float v = ( sqrt(float (u*u - 2*a*s) ) );
    
     if(v<=V)
     cout<<"Yes"<<"\n";
     
     else
     cout<<"No"<<"\n";
   
  }
    
    
}
return 0;

}

@iceknight1093
1
1 100 20 20

For this case, the answer is no.

Why would it be No?
Chef is driving at 1 m/s, and wants to be at \leq 100 m/s after 20\ m.
He can achieve that by simply not decelerating at all.
In general if u\leq v the answer is going to be Yes, irrespective of what a and s are.

Oh okay i got that.
Thanks @iceknight1093

Thanks a lot, i had assumed u^2-2as would be always be +ve as v<u, made such a horrible silly mistake.

Thank you! I really didn’t thought about this case.

Even using BufferReader also it can’t be solved under the time limit as there is no way in java no one can solve it under the time limit of 0.5 sec

well i think the time limit which is given there is general, it varies language to language.

Below solution is accepted in 0.52 Seconds.

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out);
        int t = Integer.parseInt(inp.readLine());
        while (t-- > 0) {
            String[] uvas = inp.readLine().split(" ");
            int u = Integer.parseInt(uvas[0]);
            int v = Integer.parseInt(uvas[1]);
            int a = Integer.parseInt(uvas[2]);
            int s = Integer.parseInt(uvas[3]);
            double vel = Math.sqrt(Math.pow(u, 2) - (2 * a * s));
            if (Math.pow(u, 2) - (2 * a * s) < 1) {
                out.write("YES\n");
            } else if (vel <= (double) v) {
                out.write("YES\n");
            } else {
                out.write("NO\n");
            }
            out.flush();
        } 
	}
}