题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2438
题目大意:
转弯,给定汽车的长度L和宽度D,以及转弯处马路的宽度X和Y,问汽车能否从这里转过去。
解题思路:
这个题目应该考虑一些极限情况,就是汽车靠着外侧角的两条边行驶过去。
如上图所示。那么我们可以求得汽车外侧的直线的方程,即图中红色的直线的方程。
y=xtan(t)+L*sin(t)+D/cos(t);
马路内侧点的坐标为(-Y,X)。将该点的x坐标带入方程可以得到y关于角度的函数:
y=-Y*tan(t)+L*sin(t)+D/cos(t);
y关于t是一个凸函数,通过三分我们可以求得y的最大值,
然后将该最大值与题目给定的X进行比较,得到结果。
源代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const double eps=1e-8;
const double PI=acos(-1.0);
double X,Y,L,D; //马路的宽度,汽车的长和宽
double l,r,ll,rr;
double cal(double t) //角度
{
return Y*tan(t)*(-1)+L*sin(t)+D/cos(t);
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%lf%lf%lf%lf",&X,&Y,&L,&D)==4)
{
l=0;
r=PI/2;
if(D>X || D>Y) //这个地方的特判是不可少的
{
printf("no\n");
continue;
}
while(r-l>eps)
{
ll=l+(r-l)/3;
rr=l+(r-l)*2/3;
if(cal(ll)<cal(rr))
l=ll;
else
r=rr;
}
if(cal(r)<=X) //坐标
printf("yes\n");
else
printf("no\n");
}
return 0;
}