hihocoder #1142 : 三分·三分求极值
链接:http://hihocoder.com/problemset/problem/1142
思路:
三分模板题
实现代码:
#include<bits/stdc++.h> using namespace std; #define ll long long const int M = 1e5 + 10; #define eps 1e-8 int a,b,c,x,y; double getsum(double x1){ //获取y的值 return a*x1*x1+b*x1+c; } double getdis(double k){ //当前点离p点的距离 return (k-x)*(k-x)+(getsum(k)-y)*(getsum(k)-y); } void Three_search(){ double l = -200, r = 200; while(l + eps < r){ double m1 = l+(r-l)/3; double m2 = r-(r-l)/3; if(getdis(m1) < getdis(m2)) r = m2; else l = m1; } double ans = getdis(l); ans = sqrt(ans); printf("%.3lf\n",ans); } int main() { cin>>a>>b>>c>>x>>y; Three_search(); }