Description
The New Year garland consists of N lamps attached to a common wire that hangs down on the ends to which outermost lamps are affixed. The wire sags under the weight of lamp in a particular way: each lamp is hanging at the height that is millimeter lower than the average height of the two adjacent lamps.
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 = (Hi- + Hi+)/ - , for all < i < N
HN = B
Hi >= , for all <= i <= N The sample garland with lamps that is shown on the picture has A = and B = 9.75.
Input
The input file consists of a single line with two numbers N and A separated by a space. N ( <= N <= ) is an integer representing the number of lamps in the garland, A ( <= A <= ) 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
532.81
Sample Output
446113.34
Source
根据所有 Hi >= 0 这个条件进行二分枚举第二个点的值,最后计算出最后一个点的值
判断 if(num[i]<zero) 时,刚开始直接<0,导致错误,后来用了高精度zero(#define zero 1e-10),所以细节很重要
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define inf 1e12
#define N 1006
#define zero 1e-10
int n;
double A;
double num[N];
bool solve(double mid){
num[]=mid;
for(int i=;i<n;i++){
num[i]=*num[i-]+-num[i-];
if(num[i]<zero){
return false;
}
}
return true;
}
int main()
{ while(scanf("%d%lf",&n,&A)==){
num[]=A;
double low=-inf;
double high=inf;
for(int i=;i<;i++){
double mid=(low+high)/;
if(solve(mid)){
high=mid;
}
else{
low=mid;
}
}
printf("%.2lf\n",num[n-]);
}
return ;
}