zoukankan      html  css  js  c++  java
  • hihocoder #1142 : 三分·三分求极值

    时间限制: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位小数(四舍五入)

    题解:

    三分法板子题,我们可以明显看出P(x,y)到抛物线距离是个凸函数,所以存在极值

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #define RG register
     8 #define il inline
     9 using namespace std;
    10 const double eps=1e-5;
    11 double a,b,c,x,y;
    12 double f(double i){
    13     double j=a*i*i+b*i+c;
    14     return sqrt((x-i)*(x-i)+(y-j)*(y-j));
    15 }
    16 void work()
    17 {
    18     double l=-10000.0,r=10000.0,lmid,rmid;
    19     while(l<r-eps){
    20         lmid=l+(r-l)/3;rmid=r-(r-l)/3;
    21         if(f(lmid)<f(rmid))r=rmid;
    22         else l=lmid;
    23     }
    24     printf("%.3lf
    ",f(l));
    25 }
    26 
    27 int main()
    28 {
    29     while(cin>>a>>b>>c>>x>>y)
    30     work();
    31     return 0;
    32 }
  • 相关阅读:
    1.17 Python基础知识
    反射
    面向对象——类
    异常处理
    面向对象——静态方法、类方法、属性法法,其他特殊方法
    面向对象——继承
    question
    configparser模块
    hashlib模块
    shelve模块
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7375375.html
Copyright © 2011-2022 走看看