HDU 2438 Turn the corner【三分】

题目大意:
给出汽车当前的街道宽度为x,要转弯进入的街道宽度为y,给出汽车的长度和宽度,为能否通过
解题思路:如果给我一道三分的题目,我还真的不知道往哪里去想。看到这个大牛的博客才知道思路是怎样的
http://hi.baidu.com/novosbirsk/item/b52cd716a2068d463a176e07 

车转弯的时候车有段与地面的夹角角度是从0度变化到90度的。也就是转弯的时候需要一个最大的宽度才能过去。 

否则就卡在 那里了。这个宽度PH是先增加后减少的。是个凸型函数,因此是三分求的极值。

直线y的斜率为tan(θ),还经过点(0, Lsin(θ)+D/cos(θ))因此得到y的直线方程。

y=xtan(θ)+Lsin(θ)+D/cos(θ) 

求的PH就是当y=X(汽车当前在的街道的宽度)时,解出的x的值的绝对值。

-x=|x|=(lsinθ+w/cosθ-x)/tanθ; (l==L, D==w)

代码如下:

View Code
/*
HDU 2438 Turn the corner
*/
#include<stdio.h>
#include<math.h>
#define pi acos(-1)
double x, y, l, w;
double cal(double t)
{
    return (l*sin(t)+w/cos(t)-x)/tan(t);
}
int main()
{
    while(scanf("%lf%lf%lf%lf", &x, &y, &l, &w)!=EOF)
    {
        double left=0, right=pi/2,  mid, midmid;
        while(left+1e-9<=right)
        {
            /*
            mid=(left+right)/2;
            midmid=(mid+right)/2;  //这样就是WA的,真心不懂... 
            */
            mid=(right-left)/3+left;
            midmid=(right-left)*2/3+left;
            if(cal(mid)>=cal(midmid))
                right=midmid;
            else
                left=mid;
        }
        if(cal(mid)-y>0)
            printf("no\n");
        else
            printf("yes\n");
    }
    return 0;
}

 

 

 

posted on 2013-03-02 15:14  crying_Dream  阅读(472)  评论(0编辑  收藏  举报