luogu:https://www.luogu.com.cn/problem/P3382
给出一个 \(n\) 次函数,保证在范围 \([l, r]\) 内存在一点 \(x\),使得 \([l, x]\) 上单调增,\([x, r]\) 上单调减。求出 \(x\) 的值。
#include <bits/stdc++.h>
using namespace std;
#define eps 1e-8
const int N = 15;
int n;
double l, r, a[N];
double f(double x){
double ans = 0;
for (int i = 0; i <= n; i ++ )
ans = ans * x + a[i];
return ans;
}
int main(){
cin >> n >> l >> r;
for (int i = 0; i <= n; i ++ )
cin >> a[i];
while (r - l > eps){
double mid1 = l + (r - l) / 3.0, mid2 = r - (r - l) / 3.0;
if ( f(mid1) > f(mid2) )
r = mid2;
else
l = mid1;
}
printf("%.5lf\n", l);
return 0;
}