/* Lab : Mock Test 01 Day : Tuesday [ 16 Sept 08 ] Problem Number: 02 Problem Title : Minimum Travel Time of a Sub-Way Train Theory : when train starts with initial velocity u=0 and reaches to maximum velocity v=maxVelocity, with constand acceleration a=maxAcceleration. the time taken to reach maximum velocity can be computed by the formula v = u + a*t i.e. t = maxVelocity / maxAcceleration in achieving maximum speed the distance traveled by the train can be calculated by s = u*t + 0.5*a*t^2 i.e. s = 0*t + 0.5*a*t^2 s = 0.5 * maxAcceleration * t * t s = 0.5 * maxAcceleration * ( maxVelocity / maxAcceleration ) * ( maxVelocity / maxAcceleration ) s = 0.5 * ( maxVelocity * maxVelocity ) / maxAcceleration The train journey has three parts 1. s distance with acceleration a in time t 2. ( distance -2s ) with constant speed in time ( distance -2s ) / maxVelocity 3. s distance with retardation a in time t Total travel time is sum of all three Important Note: It is also possible that the distance between two stations is not enough to speed up the train up to its maximum speed. Q: When this will happen ? A: Time taken to attain maximum speed by train t = maxVelocity / maxAcceleration distance traveled in this duration (use s = u*t + 0.5*a*t*t) ( 0.5 * maxVelocity * maxVelocity / maxAcceleration ) if the distance between two consecutive station is less than 2 * ( 0.5 * maxVelocity * maxVelocity / maxAcceleration ) that is ( maxVelocity * maxVelocity / maxAcceleration ) then train can not attain its maximum speed . for this case the train starts from speed 0 accelerates with speed maxAcceleration until it reaches to the half of distance b/w two stations and then retards with same acceleration maxAcceleration until it stops. Q: What is time taken to go up to half of distance b/w two stations A: using formula s = u*t + 0.5*a*t*t; as u = 0 the formula becomes s = 0.5*a*t*t; or as t = Squire root of ( 2*s/a ) here s=distance/2; a=maxAcceleration; therefore t = Squire root of ( 2*(distance/2) / maxAcceleration ) t = Squire root of ( distance / maxAcceleration ) In Java we can use function Math.sqrt for squire root of therefore t = Math.sqrt( distance / maxAcceleration ) Since this is time to cover half the distance, therefore time to cover full distance 2 * Math.sqrt( distance / maxAcceleration ) */ class sol_MockTest01_prob02{ public static void main(String args[]) { int distance, maxVelocity, maxAcceleration; double s, t_acceleration, t_constantSpeed, t_total; distance = Integer.parseInt(args[0]) ; maxVelocity = Integer.parseInt(args[1]) ; maxAcceleration = Integer.parseInt(args[2]) ; // First check whether the distance b/w two stations // is not sufficient to attain maximum speed by train if( ( maxVelocity * maxVelocity / maxAcceleration ) > distance ) { t_total = 2 * Math.sqrt( distance / maxAcceleration ); } else { // Time to accelerate t_acceleration = ( double ) maxVelocity / maxAcceleration ; s = ( 0.5 * maxVelocity * maxVelocity ) / maxAcceleration ; // Time of constant journy t_constantSpeed = ( double ) ( distance - 2*s ) / maxVelocity ; // keeping in mind acceleration and retardation time are same t_total = t_acceleration + t_constantSpeed + t_acceleration ; } System.out.println(" Minimum journey time is "+ t_total + " sec"); } }