题意:有n个底面是正方形的四棱锥,用一个水平截面将所有四棱锥分成两半,要求上一半体积的和等于下一半,求水平截面的高度,输出整数部分。

解法:二分截面高度。比赛的时候二分写不明白了orz……

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
double const eps = 1e-3;
double a[10005], b[10005];
int n;
int cal(double mid)
{
double sum1 = 0.0, sum2 = 0.0;
for(int i = 0; i < n; i++)
{
if(mid > a[i])
{
sum2 += a[i] * b[i] * b[i] / 3.0;
continue;
}
double vsum = a[i] * b[i] * b[i] / 3.0;
double x = (a[i] - mid) * b[i] / a[i];
double lvsum = (a[i] - mid) * x * x / 3.0;
sum1 += lvsum;
sum2 += vsum - lvsum;
}
if(fabs(sum1 - sum2) < eps) return 0;
if(sum1 > sum2) return 1;
return -1;
}
int solve()
{
double l = 0.0, r = 1000.0;
double mid;
while((r-l)>= eps)
{
mid = (l + r) / 2.0;
int tmp = cal(mid);
if(tmp == 0) return mid;
else if(tmp == 1) l = mid + eps;
else r = mid - eps;
}
return mid;
}
int main()
{
int T;
while(~scanf("%d", &T))
{
while(T--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%lf", &a[i]);
for(int i = 0; i < n; i++)
scanf("%lf", &b[i]);
printf("%d\n", (int)solve());
}
}
return 0;
}

  

while(r - l > 1)
{
LL mid = (l + r) >> 1;
if(judge(mid)) l = mid;
else
r = mid;
}
cout << l << endl;
/*贴一个实验室大神的二分模板*/

  

HDU 5432 Pyramid Split的更多相关文章

  1. hdu 5432 Pyramid Split 二分

    Pyramid Split Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/conte ...

  2. hdu 5432 Pyramid Split(二分搜索)

    Problem Description Xiao Ming is a citizen who's good at playing,he has lot's of gold cones which ha ...

  3. HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))

    http://acm.hdu.edu.cn/showproblem.php?pid=5423 题目大意:给你一个树 判断这棵树是否是独特的 一颗树是独特的条件:不存在一颗和它本身不同但相似的树 两颗树 ...

  4. hdu5432 Pyramid Split

    Problem Description Xiao Ming is a citizen who's good at playing,he has lot's of gold cones which ha ...

  5. hdu 5432

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  6. hdu5432 二分

    Pyramid Split Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. CVPR2021|一个高效的金字塔切分注意力模块PSA

    ​ 前言: 前面分享了一篇<继SE,CBAM后的一种新的注意力机制Coordinate Attention>,其出发点在于SE只引入了通道注意力,CBAM的空间注意力只考虑了局部区域的信息 ...

  8. 【二分法】 HDU 2446 Shell Pyramid

    意甲冠军:非常多,形成一个金字塔球 文章x层 x*(x+1)/ 2 球 给你个S 金字塔的一层代表第一数字向下S球 它是其中  这层中的第几行 第几列 公式 1 : x*(x+1)*(x+2)/ 6 ...

  9. hdu 2191 珍惜现在,感恩生活

    链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2191 思路:多重背包模板题 #include <stdio.h> #include ...

随机推荐

  1. POJ2004 Mix and build Trie树? dp?

    学习Trie树中,所以上网搜一下Trie树的题,找到这个,人家写着是简单dp,那我就想着能学习到什么Trie树上的dp,但最后发现根本好像跟Trie树没有什么联系嘛... 题意就是给你很多个字符串(长 ...

  2. Unique Binary Search Tree II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. Elasticsearch 学习~

    http://cloud.51cto.com/art/201505/476322.htmEs https://www.gitbook.com/book/asdgh000/mongodb-elastic ...

  4. JAVA WEB中如何让数据库连接对开发人员完全透明?

    书上的技术,确实开了眼界. 列相关测试代码如下,慢慢体会开发的模式. PropsUtil package org.smart4j.chapter2.util; import java.io.FileN ...

  5. g++ gcc

    编写一个main.cpp,linux 下执行步骤: (1)g++ mian.cpp ./a.out 后缀名是.cpp,编译完成后的可执行文件一般是a.out,也可以自己指定. (2) 编译命令(C): ...

  6. [hackerrank]John and GCD list

    https://www.hackerrank.com/contests/w8/challenges/john-and-gcd-list 简单题,GCD和LCM. #include <vector ...

  7. eclipse中(装了插件m2eclipse后的)导入maven工程显示"感叹号"

      有时候导入一些开源工程(maven结构的),在eclipse中(装了插件m2eclipse后的)该工程前面会有一个小的红色感叹号,但点开工程后没有发现有打小红叉的内容,这种情况其实大部分是jar包 ...

  8. stringUtils是apache下的Java jar补充包

    org.apache.commons.lang.StringUtils StringUtils中一共有130多个方法,并且都是static的, 所以我们可以这样调用StringUtils.xxx().

  9. java:静态成员变量和静态函数

    静态成员变量 可以使用类名调用,如 class Dog { static int age; } class Test2{ public static void main(String args[]){ ...

  10. JDBC学习总结(四)

    JDBC对LOB的读写 在JDBC中提供了java.sql.Blob和java.sql.Clob,两个类分别代表BLOB和CLOB数据.         · BLOB(Binary Large Obj ...