Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
题解:函数是凸性的,用二分搜索需要做特殊处理,求解凸性函数用三分搜索是比较方便的。

类似二分的定义Left和Right,mid = (Left + Right) / 2,midmid = (mid + Right) / 2;
如果mid靠近极值点,则Right = midmid;否则(即midmid靠近极值点),则Left = mid;
对于求最大极值,if(sum_mid>sum_midmid)
r=midmid-1e-8;
r=midmid-1e-8;
对于求最小极值 ,if(sum_mid<sum_midmid)
r=midmid-1e-8;
r=midmid-1e-8;
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
#define maxn 10001
#define INF 2147483646
using namespace std;
#define eps 1e-7
double mid_pow(double u,double y)
{
return (6*pow(u,7.0)+8*pow(u,6.0)+7*pow(u,3.0)+5*pow(u,2.0)-y*u);
}
int main()
{
int T;
double y,l,r,x,mid,midmid;
cin>>T;
while(T--)
{
cin>>y;
l=0,r=100;
while(r-l>eps)
{
mid=(l+r)/2.0;
midmid=(mid+r)/2.0;
double sum_mid=mid_pow(mid,y);
double sum_midmid=mid_pow(midmid,y);
if(sum_mid<sum_midmid)//求解最小极值
r=midmid-1e-8;
else
l=mid-1e-8;
}
printf("%.4lf\n",mid_pow(midmid,y));
}
}