题目描述:给出数列H的首项A,通向公式为h[i]=(h[i-1]+h[i+1])/2-1,求第n项的最小值。
题解:先将公式变形:h[i+1]=2*h[i]+2-h[i-1],通向公式都给出了,只需枚举第二项,即可递推出,很明显第二项越小,最后的值才会变小,二分第二项即可。
参考程序:
#include<cstdio>
#define maxA 1100
#define maxn 1100
using namespace std;
int n;
double B;
double h[maxn];
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]<0)return false;
}
B=h[n-1];
return true;
}
int main(){
double A;
scanf("%d%lf",&n,&A);
h[0]=A;
double l=-1,r=maxA+16;
for (int i=0;i<100;i++){
double mid=(l+r)/2;
if (Check(mid))r=mid;
else l=mid;
}
printf("%.2f",B);
return 0;
}