ZOJ 3203 Light Bulb( 三分求极值 )


**链接:****传送门 **

题意: 求影子长度 L 的最大值

思路:如果 x = 0 ,即影子到达右下角时,如果人继续向后走,那么影子一定是缩短的,所以不考虑这种情况。根据图中的辅助线外加相似三角形定理可以得到 L = D * (h-x)/(H-x) + x , 再经过一些变形后可知这个 L = D * ( H - h )/( x - H ) + ( x - H ) + H + D ,明显的对号函数在左侧的图像,所以一定是个 凸函数 ,用三分求出极值即可


/*************************************************************************
    > File Name: zoj3203.cpp
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年05月05日 星期五 18时53分33秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cstdio>
using namespace std;

#define eps 1e-6
#define dou double
dou H,h,D;
int T;

dou  f(dou x){
	return D*(h-x)/(H-x) + x;
}
void solve(){
	// 一定要注意边界的选取,不能随意乱选!!
	dou l = 0 , r = h , mid , midmid;
	while(r-l>eps){
		mid = (l+r)/2;
		midmid = (mid+r)/2;
		if( f(mid)>=f(midmid) )	r = midmid;
		else					l = mid;
	}
	dou ans = f((r+l)/2);
	// cout<< fixed << setprecision(3) << ans <<endl;
	printf("%.3lf\n",ans);
}
int main(){
	scanf("%d",&T);
	while(T--){
		cin>> H >> h >> D;
		solve();
	}	
	return 0;
}
posted @ 2017-05-05 22:52  ojnQ  阅读(364)  评论(0编辑  收藏  举报