I can't cross the Highway...Help!

With regard to this time’s Long Challenge and the question Highway crossing.

I feel that I have got my logic on point but seem to be missing some minute detail. I pass all the tests except the second subtask’s second part. I feel my code is almost self explanatory.

I’ve gone over my code about 20 times but can’t see what I’m missing!

I’ve solved taking normal X,Y axis with cars moving on X axis and Chef going downwards in negative Y direction hence the coordinates.

The submission link as well - https://www.codechef.com/viewsolution/18214679

Thanks In Advance :slight_smile:

#include <iostream>
#include<stdio.h>
#include<bits/stdc++.h>

#define ll long long int
#define li long int

using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    ll t;
    cin>>t;
    while(t--)
   {

//double g=-10000000000.12432;
//cout<<setprecision(16)<<g;
     ll n,s,y;
     cin>>n>>s>>y;

     double time_to_cross_lane=double(y)/double(s);
     vector<ll> velocities(n);
     vector<ll>directions(n);
     vector<ll>positions(n);
     vector<ll>lengths(n);

     for(ll i=0;i<n;i++)
     {

         cin>>velocities[i] ;
     }

     for(ll i=0;i<n;i++)
     {
         cin>>directions[i] ;
     }
     for(ll i=0;i<n;i++)
     {
         cin>>positions[i] ;
     }

     for(ll i=0;i<n;i++)
     {
         cin>>lengths[i] ;
     }

     double total_time=0.0;
     for(ll i=0;i<n;i++)
     {

         //left to right
         if(directions[i]==1)
         {

            // coordinates of car. left and right at any time
            
             double left_coordinate =(positions[i]-lengths[i])+velocities[i]*(total_time);
             double right_coordinate =(positions[i]          )+velocities[i]*(total_time);

             // case 1 not hitting
             if(left_coordinate>=0 && right_coordinate>=0)
             {
                 total_time+=time_to_cross_lane;
                 continue;
             }
             //case 2
             else if(left_coordinate<=0 && right_coordinate>=0)
             {
                 total_time+=(double(-left_coordinate)/double(velocities[i])) + time_to_cross_lane;
                 continue;
             }
             //case 3
             else if(left_coordinate<=0 && right_coordinate<=0)
             {
                 double time_to_reach_for_car=(double(-right_coordinate)/double(velocities[i]));
                 double time_to_cross_length = double(lengths[i])/double(velocities[i]);
                // if car reaches before chef would cross the lane
                 if( time_to_reach_for_car<=time_to_cross_lane)
                 {

                    total_time+=time_to_reach_for_car+time_to_cross_length+time_to_cross_lane;
                 }
                 else
                      total_time+=time_to_cross_lane;

             }
             // if a coordinate is zero
             else if( left_coordinate==0 || right_coordinate ==0 )
             {
                 if(left_coordinate==0)
                {
                    total_time+=time_to_cross_lane;
                }
                else if(right_coordinate==0)
                {
                    total_time+=time_to_cross_lane+(double(lengths[i])/double(velocities[i]));
                }


             }
             else
                total_time+=time_to_cross_lane;



         }
         //right to left
         else if(directions[i]==0)
         {

             double left_coordinate =(positions[i])-(velocities[i]*total_time);
             double right_coordinate =(positions[i]+lengths[i])-(velocities[i]*total_time);

             // case 1 not hitting
             if(left_coordinate<=0 && right_coordinate<=0)
             {
                 total_time+=(time_to_cross_lane);
             }
             else if(left_coordinate<=0 && right_coordinate>=0)
             {
                 total_time+=(double(right_coordinate)/double(velocities[i]) + time_to_cross_lane);

             }
             
             else if(left_coordinate>=0 && right_coordinate>=0)
             {

                 double time_to_reach_for_car=(double(left_coordinate)/double(velocities[i]));
                 double time_to_pass =(time_to_reach_for_car+( double(lengths[i])/double(velocities[i])));

                 //car would reach before chef would cross lane
                 if( time_to_reach_for_car<=time_to_cross_lane)
                 {
                     total_time+=(time_to_pass+time_to_cross_lane);
                 }
                 else
                  total_time+=(time_to_cross_lane);

              }
              //coordinate is zero
             else if(left_coordinate==0||right_coordinate==0)
             {
                if(left_coordinate==0)
                {
                    total_time+=(time_to_cross_lane+( double(lengths[i])/double(velocities[i])));
                }
                else if(right_coordinate==0)
                {
                    total_time+=(time_to_cross_lane);
                }

             }



         }
      }
   cout<<fixed<<setprecision(12)<<total_time<<'\n';


   }

    return 0;
}

Tend to give submission link to the code rather than pasting it here.

The parts where the time taken so far being compared, try changing it to a precision based difference calculation for 10^-6. I think that would work. I also faced the same WA at the second subtask’s second part.

My submission ids would help here. WA solution, the one without precision difference and precision comparison and AC solution, the one with precision difference and precision comparison.

If this doesn’t give you AC, please ping back here we can further discuss why you were not able to cross the highway :wink:

1 Like

Your solution will get AC. To consider that 0.000001 just increase or decrease the coordinate of front position of car accordingly. IF You have a any doubt let me know. WishYouHighRating

1 Like

Thanks a lot. The precision thing worked for me! I’ve crossed the Highway! :slight_smile:

Thanks a lot. It worked for me :slight_smile: