zoukankan      html  css  js  c++  java
  • HDU 5432 Pyramid Split 二分

    题目链接:

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

    Pyramid Split

    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 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
     

    题解:

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

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

    代码:

     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
    ",low);
    39     }
    40     return 0;
    41 }
    View Code
  • 相关阅读:
    MyBatisPartA
    概念:漏洞扫描技术
    概念:防火墙技术
    概念:认证技术与访问控制
    概念:为什么要引进密钥管理技术
    概念:数字签名、传统签名和认证
    概念:简述对称密码算法和公钥密码算法的区别
    概念:单向散列函数部分知识点
    Redis单线程QPS效率高的原因,以及redis6.0升级后的变化
    Mydql数据库缓存池Buffer Pool 冷热数据分离
  • 原文地址:https://www.cnblogs.com/fenice/p/5262116.html
Copyright © 2011-2022 走看看