ZOJ 3203 Light Bulb 三分

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   cti   

最簡單的三分題,期末考完先做一道練練手

就是這麼一個圖,告訴你H h D,問你L最長是多少,假設人到燈的距離是X,那麼容易得到

L = H-D/x*(H-h)+D-x,求個導很容易發現是一個關於x 的凸性函數,就可以三分啦

要注意的是三分的時候的精度eps,這題要求得是1e-9才能A,1e-8都WA,真是囧

#include <cstdio>#include <sstream>#include <fstream>#include <cstring>#include <iostream>#include <algorithm>#include <map>#include <cctype>#include <ctime>#include <set>#include <climits>#include <vector>#include <queue>#include <stack>#include <cstdlib>#include <cmath>#include <string>#include <list>#define INPUT_FILE "in.txt"#define OUTPUT_FILE "out.txt"using namespace std;typedef long long LL;const int INF = INT_MAX / 2;const double eps = 1e-9;void setfile() {    freopen(INPUT_FILE,"r",stdin);    freopen(OUTPUT_FILE,"w",stdout);}double f(double H,double h,double D,double x) {    return H - D / x * (H - h) + D - x;}//三分搜尋double tsearch(double H,double h,double D) {    double l = D * (H - h) / H,r = D,mid,midr;    while(fabs(l - r) > eps) {        mid = (l + r) / 2;        midr = (mid + r) / 2;        if(f(H,h,D,mid) > f(H,h,D,midr)) {            r = midr;        } else l = mid;    }    return f(H,h,D,l);}int main() {    int T; scanf("%d",&T);    double H,h,D;    for(int kase = 1;kase <= T;kase++) {        scanf("%lf%lf%lf",&H,&h,&D);        printf("%.3f\n",tsearch(H,h,D));    }    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.