TRICKTRT - Trick or Treat - SPOJ Solution C++

  Problem Link : TRICKTRT  


👉 Hint : Use binary search technique

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
double f(double pnt,vector<pair<double,double> > &v,int n)
{
	double d,maxi=0;
	for(int i=0;i<n;i++)
	{
		double p1=pow(v[i].first-pnt,2);
		double p2=pow(v[i].second,2);
		double sum=sqrt(p1+p2);
		maxi=max(sum,maxi);
		
	}
	
	return maxi;
}
int main()
{
	
	int n;
	while(scanf("%d",&n) && n)
	{

		vector<pair<double,double> >v;
		double x,y;
		for(int i=0;i<n;i++)
		{
			cin>>x>>y;
			v.push_back(make_pair(x,y));
		}
		double low,high,mid,prev,cur,next;
		low=-200001;
		high=200001;
		
		
		int k=100;
		while(k--)
		{
			mid=low+(high-low)/2;
		
			prev=f(mid-0.00001,v,n);
			cur=f(mid,v,n);
			next=f(mid+0.00001,v,n);
		
			if(prev<cur)
				high=mid;
			else if(next<cur)
				low=mid;
		}
		cout<<mid<<" "<<f(mid,v,n)<<endl;
	}
	
	return 0;
}

 

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!!