结论
凸函数求极大值点
当 f(mid) > f(mmid) 的时候,我们可以断定 mmid 一定在白点的右边
当 f(mid) < f(mmid) 的时候,我们可以断定 mid 一定在白点的左边
int l=0,r=n;
while(l<r-1)
{
int mid=l+r>>1;
int mmid=mid+r>>1;
if(f(mid)>f(mmid))r=mmid;
else l=mid;
}
return f(l)>f(r)?l:r;
--------------------------------------------------------------
double l=0,r=n;
while(r-l>eps)
{
double mid=(l+r)/2;
double mmid=(mid+r)/2;
if(f(mid)>f(mmid))r=mid;
else l=mid;
}
return f(l)>f(r)?l:r;
--------------------------------------------------------------
凹函数求极小值点
int l=0,r=n;
while(l<r-1)
{
int mid=l+r>>1;
int mmid=mid+r>>1;
if(f(mid)>f(mmid))l=mid;
else r=mmid;
}
return f(l)<f(r)?l:r;
--------------------------------------------------------------
double l=0,r=n;
while(r-l>eps)
{
double mid=(l+r)/2;
double mmid=(mid+r)/2;
if(f(mid)>f(mmid))l=mid;
else r=mmid;
}
return f(l)<f(r)?l:r;
852. 山脉数组的峰顶索引
class Solution {
public:
int peakIndexInMountainArray(vector<int>& a) {
int l=0,r=a.size()-1;
while(l<r-1)
{
int mid=l+r>>1;
int mmid=mid+r>>1;
if(a[mid]>a[mmid])r=mmid;
else l=mid;
}
return min(r,l);
}
};
洛谷 P3382
int n;
double l,r;
const int N=110;
double a[N];
double check(double x)
{
double res=1,sum=0;
for(int i=n+1;i>=1;i--)
{
sum+=res*a[i];
res*=x;
}
return sum;
}
void solve()
{
scanf("%lld%lf%lf",&n,&l,&r);
rep(i,1,n+1)scanf("%lf",&a[i]);
while(r-l>eps)
{
double mid=(l+r)/2;
double mmid=(mid+r)/2;
if(check(mid)>check(mmid))r=mmid;
else l=mid;
}
printf("%.5lf",l);
}
104. 货仓选址
int n;
const int N=100010;
int a[N];
int check(int x)
{
int sum=0;
for(int i=1;i<=n;i++)sum+=abs(a[i]-x);
return sum;
}
void solve()
{
cin>>n;
rep(i,1,n)cin>>a[i];
sort(a+1,a+1+n);
int l=a[0],r=a[n];
while(l<r-1)
{
int mid=l+r>>1;
int mmid=mid+r>>1;
if(check(mid)>check(mmid))l=mid;
else r=mmid;
}
cout<<min(check(l),check(r))<<endl;
}
4177. 曲线
int n;
const int N=100010;
int a[N],b[N],c[N];
double check(double x)
{
double res=-1e9;
for(int i=1;i<=n;i++)
res=max(res,a[i]*x*x+b[i]*x+c[i]);
return res;
}
void solve()
{
cin>>n;
rep(i,1,n)cin>>a[i]>>b[i]>>c[i];
double l=0,r=1000;
while(r-l>eps)
{
double mid=(l+r)/2;
double mmid=(mid+r)/2;
if(check(mid)>check(mmid))l=mid;
else r=mmid;
}
printf("%.4f\n",check(r));
}