[LuoguP1883]函數 三分法

來源:互聯網
上載者:User

標籤:lse   ons   name   三分法   int   cpp   org   turn   www   

三分法的模板,一直提交,一直WA

結果,今天把精度判斷從l-r>=t改成 fabs(F(l)-F(r))>=t ,過了三個點,再將 \(t\) 改成 \(10^{-7}\) 就 \(AC\) 了。。。

這給我一個十分重要的啟示:精度問題十分重要!!!

#include <iostream>#include <cstdio>#include <cmath>using namespace std;const int MAXN = 10000+5;int a[MAXN],b[MAXN],c[MAXN],n;inline double F(double x) {    double ans=a[1]*x*x+b[1]*x+c[1];    for(int i=2; i<=n; i++) ans = max(ans ,a[i]*x*x+b[i]*x+c[i]);    return ans;}int main() {    int T;    cin >> T;    while(T--) {        cin >> n;        for(int i=1; i<=n; i++) cin >> a[i] >> b[i] >> c[i];        double l=0,r=1000,t=1e-7;        while(fabs(F(l)-F(r))>t) {            double p=(2*l+r)/3,q=(l+2*r)/3;            if(F(p) > F(q)) {                l=p;            } else r=q;        }        printf("%.4lf\n",F(l));    }    return 0;}

[LuoguP1883]函數 三分法

聯繫我們

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