Home 三分模版
Post
Cancel

三分模版

2022南京赛前,思考了一下三分经常写挂,所以还是系统整理一下比较好…(希望南京如愿拿银)

整数三分

1
2
3
4
5
6
7
8
9
// 求凹函数的极小值
int l = 1, r = 100;
while(l<r) {
	int lmid = l+(r-l)/3, rmid = r-(r-l)/3;
	lans = cal(lmid), rans = cal(rmid);
	if(lans <= rans) r = rmid-1;
	else l = lmid+1;
}	
cout << min(lans, rans) << endl;

实数三分

1
2
3
4
5
6
7
8
double l, r;
const double eps = 1e-8;
while(r-l>eps) {
	double mid = (l+r)/2.0;
	if(f(mid-eps)<f(mid+eps)) l = mid;
	ese r = mid;
}
printf("%.5lf", l);
This post is licensed under CC BY 4.0 by the author.

Pet Classification

Kruskal重构树

Comments powered by Disqus.