Monday, 17 August 2015

Radiator Springs

Standard
We all know the town Radiator Springs, connected to California by direct road (Route no. 66). After a commendable performance by Lighting McQueen at the Piston Cup, the town of Radiator Springs became famous. In order to improve the traffic capacity of the road, the town's judge and doctor, Doc Hudson ordered to place on traffic sign, thus limiting the maximum speed. The traffic sign in the town of Radiator Springs seems to be a bit odd. The reason being, they only limit the speed where the traffic signals are placed and just after passing the sign the cars are allowed to drive at any speed.
It is given that the cars of Radiator Springs has an average accerleration/deceleration of x km/h2, and has a maximum speed of y km/h. The road has the length of L km, and the speed sign, limiting the speed to w km/h, is placed d km (1 <= d <= L) away from the Radiator Springs. You have to find the minimum time that an average Radiator Spring car will need to get from the California, if the car drives at the optimal speed.
Note : The car has a zero speed at the beginning of the journey.
Input format
The first line of the input file contains two integer numbers x and y (1 <= x, y <= 10000). The second line contains three integer numbers L, d and w (2 <= L <= 10000; 1 <= d < L; 1 <= w <= 10000).
Output format
Print the answer with nine digits after the decimal point.
Partial Scoring
This problem has 20 test input files,each of them having score of 5. It means even if 10 input are passed, you will get score as 50 and solution will be marked as accepted. But If the total time limit or memory limit exceeds, then the submission is marked unaccepted even if few testcases might have passed.

Sample Input
1 1
2 1 3
Sample Output
2.500000000

Time Limit: 3 sec(s) for all input files combined.
Memory Limit: 256 MB
Source Limit: 1024 KB 
 
Code: 
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;


class TestClass {
public static void main(String[] args) {
        // TODO Auto-generated method stub
        double x=0,y=0,L=0,d=0,w=0,maxspeed=0,t=0,temp=0;
        String[] s;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        DecimalFormat numberFormat = new DecimalFormat("#.000000000");
        try {
            s=in.readLine().split(" ");
            x=Double.parseDouble(s[0]);
            y=Double.parseDouble(s[1]);
            s=in.readLine().split(" ");
            L=Double.parseDouble(s[0]);
            d=Double.parseDouble(s[1]);
            w=Double.parseDouble(s[2]);
            if(y>w){
                //before d
                if((Math.pow(y,2)/2*x)+((Math.pow(y,2)-Math.pow(w,2))/2*x)<=d){
                    t=(y/x)+(d-((Math.pow(y,2)/2*x)+((Math.pow(y,2)-Math.pow(w,2))/2*x)))/y+((y-w)/x) ;
                }
                else{
                    temp = Math.sqrt((Math.pow(w,2)+(2*x*d))/2);
                    t= ((2*temp)-w)/x;
                }
                //after d
                if((Math.pow(y,2)-Math.pow(w,2))/(2*x)<=(L-d)){
                    t+=(y-w)/x+((L-d-((Math.pow(y,2)-Math.pow(w,2))/(2*x)))/y);
                }
                else{
                    t+=(Math.sqrt(Math.pow(w,2)+(2*x*(L-d)))-w)/x;
                }
                //System.out.println("hi!!!!!!");
            }
            else{
                maxspeed = y;
                if(Math.pow(maxspeed,2)/(2*x)<=L){
                   
                   
                    t=(maxspeed/x)+((L-(Math.pow(maxspeed,2)/(2*x)))/maxspeed);
                }
                else{
                    t=Math.sqrt((2*L)/x);
                    //System.out.println("hi!!!!!!");
                   
                }
            }
            System.out.println(numberFormat.format(t));
           
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

0 comments:

Post a Comment