HDU 1551 Cable master

题解:很显然的二分检索,在算法艺术上看过原题,不过这里要注意精度:

#include <cstdio>  
int n,m;  

double a[10001];  
bool test(double x){  
    int num=0,i;  
    for(i=1;i<=n;i++)num+=int(a[i]/x);  
    if(num>=m){  
        return true;  
    }else{  
        return false;  
    }  
}  
  
int main()
{  
    while(scanf("%d%d",&n,&m),n||m){  
            int i ;  
            double sum=0;  
            for(i=1; i<=n; ++i){  
            scanf("%lf",&a[i]);  
            sum+=a[i];  
        }  
        double max=sum/m,l=0,r=max;  
        while(r-l>1e-10){  
            double mid = (l+r)/2;  
            if(test(mid)){  
                l=mid+1e-11;  
            }else{  
                r=mid-1e-11;  
            }  
        }  
        printf("%.2lf\n",(l+r)/2);  
    }  
}  

 

posted @ 2014-02-09 08:30  forever97  阅读(290)  评论(0编辑  收藏  举报