HDU3756二分或三分求滿足題意的圓錐體積

來源:互聯網
上載者:User
/*******************************************************題意:求一個圓錐的最小體積,要求這個圓錐必須覆蓋所有給出的點;因為是立體的,處理起來比較麻煩,所以就抽象到一個區間來考慮;圓錐的體積V=1/3*(Pi*r*r)*h;所以影響體積的就是:h*r*r;求了半天求不出來,所以就只能考慮搜尋了;這裡二分和三分都是可以的,注意要排序下;還要用到直角三角形相似;因為這樣可以把h和r關聯起來,二分搜尋直接搜尋h就可以了;*********************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int N=10010;struct point{    double x;    double y;    double z;} p[N];double dist(const point & a)//到z軸的距離{    return sqrt(a.x*a.x+a.y*a.y);}double find_r(double h,int t){    double r=0;    for(int i=0; i<t; i++)    {        if(r<h*p[i].x/(h-p[i].z))//直角三角形相似            r=h*p[i].x/(h-p[i].z);    }    return r;}bool cmp(const point & a,const point & b){    return a.z>b.z;}void search(int t)//二分{    double eps=1e-10;    double left=p[0].z;    double right=1<<20;    while(right-left>eps)    {        double mid=(right+left)/2;        double r=find_r(mid,t);//r        double r1=find_r(right,t);        if(r1*r1*right<r*r*mid)//h*r*r            left=mid;        else            right=mid;    }    printf("%.3lf %.3lf\n",left,find_r(left,t));}void searchs(int t)//三分(隨便用哪個都行){    double eps=1e-10;    double left=p[0].z;    double right=1<<20;    while(right-left>eps)    {        double temp=(right-left)/3;        double mid1=left+temp;//h        double mid2=left+2*temp;//h        double r1=find_r(mid1,t);//r        double r2=find_r(mid2,t);//r        if(r1*r1*mid1>r2*r2*mid2)//h*r*r            left=mid1;        else            right=mid2;    }    printf("%.3lf %.3lf\n",left,find_r(left,t));}int main(){    double h,r;    //freopen("C:\\Users\\Administrator\\Desktop\\kd.txt","r",stdin);    int tcase;    cin>>tcase;    while(tcase--)    {        int t;        cin>>t;        for(int i=0; i<t; i++)        {            cin>>p[i].x>>p[i].y>>p[i].z;            p[i].x=dist(p[i]);            p[i].y=0;        }        sort(p,p+t,cmp);        search(t);    }    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.