780B. The Meeting Place Cannot Be Changed - Codeforces Solution C++

  Problem Link : 780B. The Meeting Place Cannot Be Changed 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
 
 
int n;
long long int arr[60002];
long long int speed[60002];
 
bool fun(double t)
{
  double ll=-1,lr=pow(10,12);
  double tl,tr;
 
  for(int i=1;i<=n;i++)
  {
    tl=max(arr[i]-t*speed[i],0.0);
    tr=arr[i]+t*speed[i];
    if(tl > lr || tr < ll)
      return 0;
 
    ll=max(ll,tl);
    lr=min(lr,tr);
     //   cout<<i<<" "<<ll<<" "<<lr<<" "<<endl;
  }
  if(tl > lr || tr < ll)
    return 0;
  return 1;
}
 
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++)
    cin>>arr[i];
  for(int i=1;i<=n;i++)
    cin>>speed[i];
 
  double low=0,high= pow(10,12);
  double mid;
 
  int k=1000;
  while(k--)
  {
    mid=low+(high-low)/2;
        //cout<<mid<<" "<<fun(mid)<<endl;
    if(fun(mid))
      high=mid;
    else
      low=mid;
  }
 
  cout<<fixed<<setprecision(10)<<mid;
 
 
 
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!