hdu5432 Pyramid Split

本文介绍了一种通过二分查找法来确定一组金字塔形物体被水平切割时,使得上下两部分体积相等的切割面高度的方法。利用锥体体积公式进行计算,并采用二分查找以提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
 

Input
First line: T, the number of testcases.(1T100)

Then T testcases follow.In each testcase print three lines :

The first line contains one integers n(1n10000), the number of operations.

The second line contains n integers A1,,An(1in,1Ai1000) represent the height of the ith pyramid.



The third line contains n integers B1,,Bn(1in,1Bi100) 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)
 

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

这题用到了锥体的计算公式以及二分,二分的判断条件要注意,另外,向下取整可以用printf("%d",(int)a);或者printf("%.0f",floor(a));

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x7fffffff
#define maxn 10050
#define eps 1e-8
double s[maxn],w[maxn],h[maxn],v[maxn];
int n,m;
double cal(double h1)
{
    int i,j;
    double num=0,ans;
    for(i=1;i<=n;i++){
        if(h1>h[i]){
            num+=v[i];
        }
        else{
            ans=1-(h[i]-h1)*(h[i]-h1)*(h[i]-h1)/(h[i]*h[i]*h[i]);
            num+=v[i]*ans;
        }

    }
    return num;

}


int main()
{
    int i,j,T;
    double sum,maxx;
    double l,r,mid;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        sum=0;maxx=0;
        for(i=1;i<=n;i++){
            scanf("%lf",&h[i]);
            maxx=max(maxx,h[i]);
        }
        for(i=1;i<=n;i++){
            scanf("%lf",&w[i]);
            s[i]=w[i]*w[i];
            v[i]=s[i]*h[i]/3;
            sum+=v[i];
        }
        l=0;r=maxx;
        while(r-l>eps){
            mid=(l+r)/2;
            if(cal(mid)*2>sum){
                r=mid;
            }
            else l=mid;
        }
        printf("%d\n",(int)mid);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值