#1142 : 三分·三分求极值
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
这一次我们就简单一点了,题目在此:
[week40_1.PNG]
在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d。
提示:三分法
输入
第1行:5个整数a,b,c,x,y。前三个数构成抛物线的参数,后两个数x,y表示P点坐标。-200≤a,b,c,x,y≤200
输出
第1行:1个实数d,保留3位小数(四舍五入)
样例输入
2 8 2 -2 6
样例输出
2.437
//三分极值
#include<cstdio>
#include<algorithm>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std;
template<class T>inline T read(T&x)
{
char c;
while((c=getchar())<=32)if(c==EOF)return 0;
bool ok=false;
if(c=='-')ok=true,c=getchar();
for(x=0; c>32; c=getchar())
x=x*10+c-'0';
if(ok)x=-x;
return 1;
}
template<class T> inline T read_(T&x,T&y)
{
return read(x)&&read(y);
}
template<class T> inline T read__(T&x,T&y,T&z)
{
return read(x)&&read(y)&&read(z);
}
template<class T> inline void write(T x)
{
if(x<0)putchar('-'),x=-x;
if(x<10)putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
write(x);
putchar('\n');
}
//-------ZCC IO template------
const int maxn=1000011;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007
double px,py;
double a,b,c;
double d(double x)
{
return sqrt((x-px)*(x-px)+(a*x*x+b*x+c-py)*(a*x*x+b*x+c-py));
}
DB ts(double left,double right)
{
DB lm,rm,dis;
DB y1=0,y2=0;
while(left<=right)
{
dis=(right-left)/3.0;
lm=left+dis;
rm=lm+dis;
y1=d(lm),y2=d(rm);
//printf("%lf %lf\n",left,right);
//getchar();
if(y1==y2)return y1;
else if(y1<y2)
{
right=rm;
}
else
{
left=lm;
}
}
return min(y1,y2);
}
//0.421
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
int n,m,i,j,t;
while(~scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&px,&py))
{
if(d(px)==py)
printf("%0.00\n");
else
printf("%.3lf\n",ts(-200,200));
}
return 0;
}