三分法求单峰函数最值

求单峰函数最小值;

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));(坐标系的旋转)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值