HDOJ – 3714 三分搜尋

來源:互聯網
上載者:User

     昨天編程之美的C題搞不出來..看了下別人的方法...三分搜尋水之...以前沒寫過...

    三分從二分上發展來的...當這個函數不是單峰時,也能找到最值~~好牛逼....

     每次不僅取中點mid=(l+r)/2..還取令一個次中點mmid=(mid+r)/2...如果mid上的值比mmid優...r=mmid...否則 l=mid...

    這題和昨天的C題可以說一模一樣...出原題不太好吧....

Program:

#include<iostream>#include<cmath>#include<stack>#include<queue>#include<set>#include<algorithm>#include<stdio.h>#include<string.h>#define ll long long#define oo 1000000007using namespace std; struct node{       double a,b,c;}f[10005];int n;double getdata(double x){       double m=f[1].a*x*x+f[1].b*x+f[1].c;       for (int i=2;i<=n;i++)  m=max(m,f[i].a*x*x+f[i].b*x+f[i].c);        return m;}int main(){                int T,i;        double l,r,ml,mr,dl,dr;       scanf("%d",&T);       while (T--)       {              scanf("%d",&n);              for (i=1;i<=n;i++) scanf("%lf%lf%lf",&f[i].a,&f[i].b,&f[i].c);              l=0.0;              r=1000.0;               while (r-l>1e-10)              {                    ml=(l+r)/2;                    mr=(ml+r)/2;                    dl=getdata(ml);                    dr=getdata(mr);                    if (dl>dr) l=ml;                         else r=mr;              }              printf("%.4lf\n",getdata(r));       }       return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.