-
前言
其实也没什么好说的吧,三分法就是用来求一个单调函数的最值和满足最大值的(x),秦九韶算法就是在(O(N))时间内求一个多项式值
-
怎么用
三分法使用--看这篇:https://www.cnblogs.com/Rye-Catcher/p/9255304.html
函数定义域和值域都为实数呢?
不多说,先看这道题:https://www.luogu.org/problemnew/show/P3382
三分法的代码相似,大家应该都知道.哪怎么快速求一个多项式函数的值呢?
秦九韶算法:计算(f(x)=sum^n_{i=0}a[i]x)
不太标准的伪代码:
poly=0 for i=N to 0 poly=poly*x+a[i] return poly
- 例题代码:
include
include
include
include
include
include
include
include
define ri register int
define ll long long
using namespace std;
const int maxn=25;
const int inf=0x7fffffff;
const double eps=1e-7;
template
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c'-';
x=c-48;
while(isdigit(cgetchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;
return ;
}
int n;
double l,r,a[maxn];
double f(double x){//极好用的秦九韶算法
double poly=0;
for(ri i=n;i>=0;i--){
poly=polyx+a[i];
}
return poly;
}
int main(){
scanf("%d %lf %lf",&n,&l,&r);
for(ri i=n;i>=0;i--){
scanf("%lf",&a[i]);
}
double lmid,rmid;
while(l+eps<r){
lmid=0.5(r+l),rmid=0.5*(lmid+r);
if(f(lmid)<f(rmid))l=lmid;
else r=rmid;
}
printf("%.5lf
",l);
return 0;
}