您的位置:首页 > 其它

POJ 1759 - Garland(二分搜索)

2015-03-07 15:43 225 查看
题目:

http://poj.org/problem?id=1759

题意:

最左边灯的高度是a, 有n盏灯, 求出最右边灯的最小高度.

满足: H1 = A

          Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N

          HN = B

          Hi >= 0, for all 1 <= i <= N

题意:

二分搜索, 对第一盏灯和第二盏灯的距离进行二分.

AC.

#include <iostream>
#include <cstdio>

using namespace std;
const double esp = 1e-9;
int n;
double a, h[1005], b;

bool can(double x)
{
h[1] = a-x;
for(int i = 2; i < n; ++i) {
h[i] = 2 * h[i-1] + 2 - h[i-2];
if(h[i] < 0) return false;
}
b = h[n-1];
return true;
}
void solve()
{
double l = 0, r = a;
h[0] = a;
while(r - l > esp) {
double mid = (r + l) / 2;
if(can(mid)) l = mid;
else r = mid;
}
printf("%.2lf\n", b);
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%d %lf", &n, &a)) {
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ