Hihocoder #1142: Finding the extremum by three points

Source: Internet
Author: User

"topic link": click here~~

time limit:10000ms single point time limit:1000ms memory limit:256MB
Descriptive narrative

This time we'll be a little simpler. Topics here:

in a Cartesian coordinate system, there is a parabolic y=ax^2+bx+c and a point P (x, y), which is the shortest distance d from the parabolic line to the point P.

Tip: Three-point method

input

Line 1th: 5 integer a,b,c,x,y. The first three numbers constitute the parameters of the parabolic line. The second two digits, X, Y, represent p point coordinates. -200≤a,b,c,x,y≤200

Output

Line 1th: A real number D, reserved 3 decimal places (rounded)

Example input
2 8 2-2 6
Example output
2.437
"Ideas"

As the most common method in the division, the dichotomy method is applicable to the monotone function and approximate to the value of a certain point.

However, when the function is a convex function, the dichotomy cannot be applied, then it is necessary to use the three-part method.


From the name of the tri-method we can guess that the three-part method is to make three equal points for the required approximation:

we find that LM is lower than RM, so the smallest point we're looking for must be between [LEFT,RM]. Assuming that the lowest point is between [Rm,right], it will appear that the RM has lower points around it now, which is obviously impossible. Similarly. When RM is lower than LM, the lowest point must be within the [Lm,right] range. using this property, we are able to approximate the target point at the same time as the narrowing interval, thus obtaining the extremum.

Next we go back to the topic, the distance between the parabolic line and the point can be calculated simply by the linear formula:i.e. D = min{sqrt ((x-x) ^2+ (ax^2+bx+c-y) ^2)}After the formula is expanded to 4 times, it is necessary to use the derivation method to find the extremum.

is a very troublesome thing for computer programming.
To further observe the topic, we can find that the length of D is exactly the convex function, depending on the X value brought in. the shortest distance we ask for D is exactly the extremum of this convex function. So does the three-point method just be able to solve the problem? We need to pay attention to how to divide the interval in the process of solving the problem, what is the meaning of each variable we ask.

Code:

#include <bits/stdc++.h>using namespace Std;const double Min=-1e3;const double max=1e3;const double; Double a,b,c,x,y;double Calc (double x) {    return sqrt ((x-x) * (x-x) + (a*x*x+b*x+c-y) * (a*x*x+b*x+c-y));} void Solve () {    double left=min,right=max;    Double Mid,midmid;    Double Mid_value,midmid_value;    while (Left+eps<right)    {        mid= (left+right)/2;        Midmid= (Mid+right)/2;        Mid_value=calc (mid);        Midmid_value=calc (midmid);        if (mid_value<=midmid_value) Right=midmid;        else Left=mid;    }    printf ("%.3f\n", Calc (left));} int main () {    scanf ("%lf%lf%lf%lf%lf", &a,&b,&c,&x,&y);    Solve ();}


Hihocoder #1142: Finding the extremum by three points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.