Pyramid Split
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 104 Accepted Submission(s): 50
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 n pyramids,there height and width are known,Xiao Ming wants to make them again to get two objects with the same volume.
Of course he won't simply melt his pyramids and distribute to two parts.He has a sword named "Tu Long" which can cut anything easily.
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.
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 n pyramids,there height and width are known,Xiao Ming wants to make them again to get two objects with the same volume.
Of course he won't simply melt his pyramids and distribute to two parts.He has a sword named "Tu Long" which can cut anything easily.
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: T, the number of testcases.(1≤T≤100)
Then T testcases follow.In each testcase print three lines :
The first line contains one integers n(1≤n≤10000), the number of operations.
The second line contains n integers A1,…,An(1≤i≤n,1≤Ai≤1000) represent the height of the ith pyramid.
The third line contains n integers B1,…,Bn(1≤i≤n,1≤Bi≤100) represent the width of the ith pyramid.
Then T testcases follow.In each testcase print three lines :
The first line contains one integers n(1≤n≤10000), the number of operations.
The second line contains n integers A1,…,An(1≤i≤n,1≤Ai≤1000) represent the height of the ith pyramid.
The third line contains n integers B1,…,Bn(1≤i≤n,1≤Bi≤100) represent the width of the ith 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)
(the results take the integer part,like 15.8 you should output 15)
Sample Input
2
2
6 5
107
8
702 983 144 268 732 166 247 569
20 37 51 61 39 5 79 99
Sample Output
1
98
思路:设分割平面的高度为x,可以简单推出x以上的椎体的体积为:((B * B) / (A * A)) * (A - x)^3 * 1 / 3
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; int a[10005], b[10005], n; typedef long long ll; double sum; bool check(double x) { double now = 0; for(int i = 1; i <= n; ++i) if(a[i] >= x) now += ((b[i] * b[i] * 1.0) / (a[i] * a[i])) * (a[i] - x) * (a[i] - x) * (a[i] - x); if(now < sum) return true; else return false; } int main() { int _; scanf("%d", &_); while(_ --) { int H = 0; scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); if(a[i] > H) H = a[i]; } for(int i = 1; i <= n; ++i) scanf("%d", &b[i]); sum = 0; for(int i = 1; i <= n; ++i) sum += b[i] * b[i] * a[i]; sum /= 2; double low = 0, high = H; while((int)low != (int)high) { double h = (low + high) / 2; if(check(h)) high = h; else low = h; } printf("%d ", (int)low); } return 0; }
二分一个高度h, 因为只需求整数部分,当(int)low == (int)high时,二分结束