Turn the corner
Time Limit : 3000/1000ms (Java/Other) Memory Limit :32768/32768K (Java/Other)
Total Submission(s) : 45 Accepted Submission(s) : 23
Problem Description
Mr. West bought anew car! So he is travelling around the city.<br><br>One day hecomes to a vertical corner. The street he is currently in has a width x, thestreet he wants to turn to has a width y. The car has a length l and a widthd.<br><br>Can Mr. West go across the corner?<br><imgsrc=../../../data/images/2438-1.jpg><br>
Input
Every line hasfour real numbers, x, y, l and w.<br>Proceed to the end offile.<br>
Output
If he can goacross the corner, print "yes". Print "no"otherwise.<br>
Sample Input
10 6 13.54<br>10 6 14.5 4<br>
Sample Output
yes<br>no<br>
Source
2008 Asia HarbinRegional Contest Online
题意:
给你一个拐角,两边的路的宽度分别为x,y。汽车的长和宽分别为h,w。问你这个汽车否转弯成功。
解题思路:
用三分法枚举角度,具体过程如下:
车转弯的时候车有段与地面的夹角角度是从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)
再来一张具体的图:
<span style="font-size:18px;">#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;
}
</span>