Turn the corner+hdu+三分搜索

本文探讨了在给定宽度、长度和角度的情况下,车辆如何顺利通过垂直拐角的问题,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Turn the corner

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1788    Accepted Submission(s): 672


Problem Description
Mr. West bought a new car! So he is travelling around the city.

One day he comes to a vertical corner. The street he is currently in has a width x, the street he wants to turn to has a width y. The car has a length l and a width d.

Can Mr. West go across the corner?

 

Input
Every line has four real numbers, x, y, l and w.
Proceed to the end of file.
 

Output
If he can go across the corner, print "yes". Print "no" otherwise.
 

Sample Input
10 6 13.5 4 10 6 14.5 4
 

Sample Output
yes no
解决方案:汽车转弯时可看到此图:
汽车要想能过这个弯,最好这样转弯,左侧贴近弯道内侧,右下角贴近弯道内侧,这回形成一个a角,然后转弯的过程a角从0到90过程变化,然后就看p点的位置了,若其能过得了这个弯道,其横坐标必须小于或等于y,而p点的横坐标随a点的变化是一个凸函数,所以用上三分搜索。
用上高中知识推得公式:
f(a)=l*cos(a)+(w-x*cos(a))/sin(a);
不过要有个特判,若x<w或y<w也不能通过。
code:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
double x,y,l,w;
double cal(double mid)
{
    return l*cos(mid)+(w-x*cos(mid))/sin(mid);
}
int main()
{
    while(~scanf("%lf%lf%lf%lf",&x,&y,&l,&w))
    {
        double left=0.0,right=acos(-1.0)/2,mid,midmid,midv,midmidv;
        while(fabs(right-left)>1e-8)
        {
            mid=(right+left)/2.0;
            midmid=(right+mid)/2.0;
            midv=cal(mid);
            midmidv=cal(midmid);
            if(midv>=midmidv)
            {
                right=midmid;
            }
            else left=mid;
        }
        if(cal(mid)>y||w>x||w>y)
        {
            printf("no\n");
        }
        else printf("yes\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值