hiho一下 第四十周 题目1 : 三分·三分求极值

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB
描述

这一次我们就简单一点了,题目在此:

在直角坐标系中有一条抛物线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


我不知道当作一个完整弧来看对不对,但是拆成两半肯定是可以的


一半到给定点的距离随着x的变化,y是呈现一个凸


于是就这样吧,懒死了


#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
double a,b,c;
double cnt(double x)
{
	return a*x*x+b*x+c;
}
double dis(double x1,double y1,double x2,double y2)
{
	return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}
const double eps=1e-9;
double seek(double x,double y,double l,double r)
{
	while(r-l>eps)
	{
		double ll=(l*2+r)/3,rr=(l+r*2)/3,y1=cnt(ll),y2=cnt(rr);
		if(dis(ll,y1,x,y)<dis(rr,y2,x,y))
			r=rr;
		else
			l=ll;
	}
	return dis(r,cnt(r),x,y);
}
int main()
{
	double x,y;
	cin>>a>>b>>c>>x>>y;
	double l=-b/(a*2),r=200.0,ans;
	ans=min(seek(x,y,-200.0,l),seek(x,y,l,r));
	printf("%.3f\n",ans);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值