#1142 : 三分·三分求極值 ( 三分極值 )

來源:互聯網
上載者:User

標籤:


#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 10007double 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.421int 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;}






#1142 : 三分·三分求極值 ( 三分極值 )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.