Garland
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1438 | Accepted: 638 |
Description
![](https://i-blog.csdnimg.cn/blog_migrate/7fe7f3a516a37fdb3b8e46be0b8c6a17.jpeg)
The leftmost lamp in hanging at the height of A millimeters above the ground. You have to determine the lowest height B of the rightmost lamp so that no lamp in the garland lies on the ground though some of them may touch the ground.
You shall neglect the lamp's size in this problem. By numbering the lamps with integers from 1 to N and denoting the ith lamp height in millimeters as Hi we derive the following equations:
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
The sample garland with 8 lamps that is shown on the picture has A = 15 and B = 9.75.
Input
The input file consists of a single line with two numbers N and A separated by a space. N (3 <= N <= 1000) is an integer representing the number of lamps in the garland, A (10 <= A <= 1000) is a real number representing the height of the leftmost lamp above the ground in millimeters.
Output
Write to the output file the single real number B accurate to two digits to the right of the decimal point representing the lowest possible height of the rightmost lamp.
Sample Input
692 532.81
Sample Output
446113.34
由于B的大小正比于a[2] 所以二分a[2]就可以了 如果有小于0说明不满足太小 l = mid
AC代码如下:
//
// Created by TaoSama on 2015-04-30
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n;
double A, B, a[1005];
//h2的值
bool check(double x) {
a[2] = x;
for(int i = 3; i <= n; ++i) {
a[i] = 2 * a[i - 1] + 2 - a[i - 2];
if(a[i] < 0) return false;
}
B = a[n];
return true;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
scanf("%d%lf", &n, &A);
a[1] = A;
double l = -1, r = 1e3 + 1;
for(int i = 0; i < 100; ++i) {
double mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
printf("%.2f\n", B);
return 0;
}