P3382 【模板】三分法
只有一个地方
在这个题当中,f(mid)>f(mid-eps)比f(mid)-f(mid-eps)>eps精度要高
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 10010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2020.10.13 using namespace std; long long n; long double k[N]; long double l,r,mid,eps=1e-7; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } long double f(long double x){ long double r=0; For(i,0,n) r=r*x+k[i]; return r; } signed main(){ cin>>n>>l>>r; For(i,0,n) cin>>k[i]; while(r-l>=eps){ mid=(l+r)/2.0; if(f(mid)>f(mid-eps)) l=mid; else r=mid; } printf("%.5Lf ",mid); return 0; }