题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5432
裸的二分查找,同时注意向下取整
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 5 using namespace std; 6 7 #define maxn 100000+5 8 const double exps = 1e-6; 9 10 int t,n; 11 12 struct node 13 { 14 double h,w; 15 }a[maxn]; 16 17 18 int main() 19 { 20 scanf( "%d",&t ); 21 while( t-- ) 22 { 23 scanf( "%d",&n ); 24 for( int i=0; i<n; i++ ) 25 scanf( "%lf",&a[i].h ); 26 for( int i=0; i<n; i++ ) 27 { 28 scanf( "%lf",&a[i].w ); 29 } 30 double L = 0, R = 1000, mid; 31 while( R - L > exps) 32 { 33 mid = (L + R) / 2; 34 double s, sum1 = 0, sum2 = 0; 35 for(int j = 0; j < n; j++) 36 { 37 38 if(a[j].h <= mid) s = 0; 39 else s = a[j].w * a[j].w * (a[j].h- mid) * (a[j].h - mid) * (a[j].h - mid) / (3.0 * a[j].h * a[j].h); 40 sum1 += s; 41 sum2 += a[j].w * a[j].w * a[j].h / 3.0 - s; 42 } 43 44 if(sum1 < sum2) R = mid; 45 else L = mid; 46 } 47 printf("%d ", (int)mid); 48 } 49 return 0; 50 }