求单峰函数最小值;
poj3737
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<stack>
#define ll long long
#define MAX 1000000
#define INF INT_MAX
#define eps 1e-8
#define PI 3.1415926
using namespace std;
double s;
double f(double x){
return PI*x*x*sqrt(((s*1.0-PI*x*x)/(PI*x))*((s*1.0-PI*x*x)/(PI*x)) - x*x) / 3.0;
}
int main(){
while (scanf("%lf",&s) != EOF){
double L = 0.0, R = sqrt(s*1.0/(2*PI));
while(R-L > eps){
double u = L + (R-L) / 3.0;
double v = R - (R-L) / 3.0;
// printf("%lf %lf\n",f(u),f(v));
if (f(u) > f(v)) R = v; //当f(u) > f(v)时,最大值解空间一定在[L,v]之间
else L = u;
}
printf("%.2lf\n%.2lf\n%.2lf\n",f(R),sqrt(((s*1.0-PI*R*R)/(PI*R))*((s*1.0-PI*R*R)/(PI*L)) - R*R),R);
}
return 0;
}
求单峰函数最大值;
poj3301
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<stack>
#include<set>
#define ll long long
#define MAX 1000
#define INF INT_MAX
#define eps 1e-8
#define PI 3.1415936
using namespace std;
int x[MAX],y[MAX];
int n;
double Square(double e){
double xmax = -MAX*1.0, xmin = MAX*1.0, ymax = -MAX*1.0, ymin = MAX*1.0,xt,yt;
for (int i=0; i<n; i++){
xt = x[i]*1.0*cos(e) - y[i]*1.0*sin(e);
yt = x[i]*1.0*sin(e) + y[i]*1.0*cos(e);
xmax = max(xmax,xt);
xmin = min(xmin,xt);
ymax = max(ymax,yt);
ymin = min(ymin,yt);
}
return max(xmax-xmin,ymax-ymin)*max(xmax-xmin,ymax-ymin);
}
int main(){
int T;
scanf("%d",&T);
while (T--){
scanf("%d",&n);
for (int i=0; i<n; i++){
scanf("%d%d",&x[i],&y[i]);
}
double L = 0.0, R = PI/2.0;
while (R - L > eps){
double u = L + (R-L)/3.0;
double v = R - (R-L)/3.0;
if (Square(u) < Square(v)) R = v;//当Square(u) < Square(v)时,最小值解空间一定在[L,v]之间
else L = u;
}
printf("%.2lf\n",Square(L));
}
return 0;
}
将矢量a(x,y) 逆时针旋转e之后的矢量为:(x*cos(e) - y*sin(e), x*sin(e) + y*cos(e));(坐标系的旋转)