题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5432

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 889    Accepted Submission(s): 358


Problem Description
Xiao Ming is a citizen who's good at playing,he has lot's of gold cones which have square undersides,let's call them pyramids.

Anyone of them can be defined by the square's length and the height,called them width and height.

To easily understand,all the units are mile.Now Ming has 
Now he put all pyramids on the ground (the usdersides close the ground)and cut a plane which is parallel with the water level by his sword ,call this plane cutting plane.

Our mission is to find a cutting plane that makes the sum of volume above the plane same as the below,and this plane is average cutting plane.Figure out the height of average cutting plane.
 

 

Input
First line: h pyramid.
 

 

Output
For each testcase print a integer - **the height of average cutting plane**.

(the results take the integer part,like 15.8 you should output 15)
 

 

Sample Input
2 2 6 5 10 7 8 702 983 144 268 732 166 247 569 20 37 51 61 39 5 79 99
 

 

Sample Output
1 98
 

题解:

  水题,由于结果是整数,所以二分的时候没有必要用浮点去分。

  二分的边界经常写成死循环,贴个代码,供自己以后总结。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 
 5 const int maxn=10000+10;
 6 
 7 int A[maxn],B[maxn];
 8 int n;
 9 
10 double solve(int h){
11     double ret=0;
12     for(int i=0;i<n;i++){
13         if(h<A[i]){
14             int tmp=A[i]-h;
15             double width=tmp*1.0/A[i]*B[i];
16             ret+=1.0/3*width*width*tmp;
17         }
18     }
19     return ret;
20 }
21 
22 int main(){
23     int tc;
24     scanf("%d",&tc);
25     while(tc--){
26         scanf("%d",&n);
27         double sum=0;
28         for(int i=0;i<n;i++) scanf("%d",A+i);
29         for(int i=0;i<n;i++) scanf("%d",B+i);
30         for(int i=0;i<n;i++) sum+=1/3.0*B[i]*B[i]*A[i];
31         int low=0,high=1001;
32         while(low+1<high){
33             int mid=low+(high-low)/2;
34             double tmp=solve(mid);
35             if(tmp*2>=sum) low=mid;
36             else high=mid;
37         }
38         printf("%d\n",low);
39     }
40     return 0;
41 }
View Code

相关文章: