/*
translation:
有n个灯泡挂在距离地面分别是hi的高度上。对每个hi有如下几个条件的限制:
H1 = A ;
Hi = (H i-1 + H i+1)/2 - 1, for all 1 < i < N ;
HN = B ;
Hi >= 0, for all 1 <= i <= N 求最大的B值是多少
solution:
二分枚举第二个灯泡的高度即可
note:
#:输出时记得输出%f而不是%lf,迷之贡献WA。。。
date:
2016.11.8
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1000 + 20;
double h[maxn], ans, a;
int n;
bool check(double mid)
{
h[1] = mid;
for(int i = 2; i < n; i++){
h[i] = 2 * h[i-1] + 2 - h[i-2];
if(h[i] < 1e-8) return false;
}
ans = h[n-1];
return true;
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%lf", &n, &a)){
h[0] = a;
double lb = 0, ub = 1000 + 16;
for(int i = 0; i < 100; i++){
double mid = (lb + ub) / 2;
if(check(mid)) ub = mid;
else lb = mid;
}
printf("%.2f\n", ans);
}
return 0;
}
poj1759(二分找寻最值)
最新推荐文章于 2020-05-05 20:44:16 发布