POJ1759-Garland

来源:互联网 发布:兰亭序真迹在哪里 知乎 编辑:程序博客网 时间:2024/06/05 16:41

二分好题,只要把判断在地上的灯的数量是否大于0即可

#include <cstdio>const int maxn = 1000 + 5;double h[maxn];int n;double a;//x为第二个灯的高度//cnt表示在地上的灯的数量bool test(double x) {    h[0] = a;    h[1] = x;    int cnt = 0;    for (int i = 2; i < n; i++) {        h[i] = (h[i-1] + 1) * 2 - h[i-2];        if (h[i] <= 0) {            h[i] = 0;            cnt++;        }    }    return cnt > 0;}int main(int argc, char const *argv[]) {    scanf("%d%lf", &n, &a);    double lb = 0, ub = a;    for (int i = 0; i < 100; i++) {        double mid = (lb + ub) / 2;        if (test(mid)) {            lb = mid;        } else {            ub = mid;        }    }    printf("%.2f\n", h[n-1]);    return 0;}
0 0
原创粉丝点击