chota bheem
chota bheem

Reputation: 411

Involvement of binary search/bisection in this?

I was doing this problem from lightoj judge(sorry for giving links i don't know how to add pictures).This is pure geometry based question and My appoarch was this which lead to Accepted solution.

Code

  #include <bits/stdc++.h>
  using namespace std;
  int main() {

   int t,temp;
   cin>>t;
   temp=t;
   while(t--)
   {
    double ab,ac,bc,r;
    cin>>ab>>ac>>bc>>r;
    double sq=ab*sqrt((r/(r+1)*1.0));
    printf ("Case %d: %lf\n", temp-t,sq);
   }
    return 0;
 }

But the problem is that this question was marked binary searched/bisection and I could not find a way to do this with binary search.I searched web to know how to do this but could not find a way.Can anybody help me to do this with binary search/bisection and what are general question in which we can apply bisectioning/binarysearch(except searching)

Upvotes: 0

Views: 103

Answers (1)

anubhavashok
anubhavashok

Reputation: 532

Using similar triangles, we can find a formula of the ratio ADE/ABC involving terms AD and AB. It is then trivial to find the ratio of ADE/BDEC by substituting ABC = ADE + BDEC.

We know that AD is bounded by 0 < AD <= AB. We can then use bisection to find which value of AD satisfies the ratio in the above interval. (Extra reading on bisection method: https://mat.iitm.ac.in/home/sryedida/public_html/caimna/transcendental/bracketing%20methods/bisection/bisection.html)

To sole this, we need to formulate an equation such that the exact solution for AD would produce a root for the equation. One such equation is: f(x) = ratio_act - ratio_est

// ADE/ABC = (AD/BC)^2 (By similar triangles)
// ADE/BDEC = (AD^2)/(AB^2 - AD^2)
double bisection(double AB, double ratio_act)
{
    auto f = [](double AD_est, double AB, double ratio_act){ return ratio_act - ((AD_est* AD_est/(AB*AB - AD_est*AD_est)));};
    double b = AB +1, a = 0, ratio_est, AD_est;
    cout << f(a, AB, ratio_act) * f(b, AB, ratio_act) << endl;
    do {
        AD_est = (b+a)/2;
        // as per above formula
        ratio_est = f(AD_est, AB, ratio_act);
        if (ratio_est*f(a, AB, ratio_act) < 0) {
            b = AD_est;
        } else {
            a = AD_est;
        }
    } while (abs(ratio_est - ratio_act) <= 1e-9);
    return AD_est;
}

Upvotes: 1

Related Questions