zoukankan      html  css  js  c++  java
  • hdu5432 二分

    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.
     
    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
    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时,二分结束
  • 相关阅读:
    《Three.js 入门指南》3.1.1
    《Three.js 入门指南》3.1.1
    《Three.js 入门指南》3.1.1
    Spring 框架基础(04):AOP切面编程概念,几种实现方式演示
    微服务架构案例(01):项目技术选型简介,架构图解说明
    Java描述设计模式(15):责任链模式
    数据安全管理:RSA加密算法,签名验签流程详解
    Java描述设计模式(14):解释器模式
    SpringBoot2 配置多数据源,整合MybatisPlus增强插件
    SpringBoot2 整合 Drools规则引擎,实现高效的业务规则
  • 原文地址:https://www.cnblogs.com/orchidzjl/p/4803697.html
Copyright © 2011-2022 走看看