概述:west先生要将车开过一个拐角,现在给出拐角两侧道路的长度和汽车的长度和宽度,求车是否能过去。
思路:车能过去,就是车的左侧紧贴直角点,右侧紧贴道路下方的店,使用函数得到:F(a)=lcosa-(x-w/cosa)/tana,是一个单峰函数,所以可以使用三分法。
感想:知道是三分,题意不难理解,难的是那个方程的解,写出来题就简单了。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<fstream>
#include<stdio.h>
#include<cstring>
#define PI 3.14159
const double dps = 1e-4;
double x, y, l, w;
double calution(double a)
{
return l*cos(a) + (w - x*cos(a)) / sin(a);
}
double ternary_search(double l, double r)
{
double ll, rr;
while (r - l>dps)
{
ll = (2 * l + r) / 3;
rr = (2 * r + l) / 3;
if (calution(ll)>calution(rr))
r = rr;
else
l = ll;
}
return r;
}
using namespace std;
int main()
{
ifstream cin("in.txt");
while (cin >> x >> y >> l >> w)
{
double l = 0, r = PI / 2;
double temp = ternary_search(l, r);
if (calution(temp) < y)
puts("yes");
else puts("no");
}
return 0;
}