Codeforces Round #414 B. Cutting Carrot 几何二分

来源:互联网 发布:淘宝店铺导航全屏代码 编辑:程序博客网 时间:2024/05/22 08:05

题目链接: Cutting Carrot

题目大意

一个等腰三角形, 底边长为1, 高为H, 要等分成n分, 求怎么切

思路

可以推出公式来, 懒得推, 直接二分好了, 浮点数的二分好好写

代码

#include <bits/stdc++.h>using namespace std;const int MAXN = 1100;const double eps = 1e-10;double cal(double H, double h, double L)//计算高为H, 底边长为L的等腰三角形下面高为h的等要梯形面积, H==h就是求三角形面积{    double l = L-(h*L)/H;    return (L+l)*h/2.0;}double bs(double s, double H, double L){    double low = 0, high = H, mid;    while(high-low >= eps)    {        mid = (low+high)/2;        if(cal(H, mid, L) < s) low = mid;        else high = mid;    }    return low;}int main(){    int n;    double H;    cin >> n >> H;    double S = cal(H, H, 1);    double s = S/n;    double h[MAXN];    h[0] = H;    for(int i=1; i<n; ++i)    {        h[i] = h[i-1] - bs(s, h[i-1], h[i-1]/H);    }    for(int i=n-1; i>0; --i) printf("%.12f ", h[i]);    return 0;}