HDU 2438 Turn the corner(三分查找)

托一个学弟的福,学了一下他的最简便三分写法,然后找了一道三分的题验证了下,AC了一题,写法确实方便,还是我太弱了,漫漫AC路!各路大神,以后你们有啥好的简便写法可以在博客下方留个言或私信我,谢谢了!

Turn the corner

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

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
Source
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2438
题意:
         给出街道在x轴的宽度X,y轴的宽度Y,还有车的长l和宽w,判断是否能够转弯成功。
 
题解:
        盗网上大牛一张图,画的很详细
 
      尽可能让车贴着外面的墙璧转弯,也就是图中的x轴和y轴,此时红线的方程就是图中的方程,此时p点的位置就是让y=X时解得的x值,要保证p点在Y内,也就是-x<y,假若在转弯的所有角度中都满足这个条件,那么就能转弯,分析得,-x先增大后减小,所以用三分求最大-x值。
 
下面给出AC代码:
复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 double x,y,l,w;
 4 const double eps=1e-6;
 5 const double pi=acos(-1.0);
 6 double solve(double angle)
 7 {
 8     return (-x+l*sin(angle)+w/cos(angle))/tan(angle);
 9 }
10 int main()
11 {
12     while(scanf("%lf%lf%lf%lf",&x,&y,&l,&w)!=EOF)
13     {
14         double ll=0,rr=pi/2,midx,midy;
15         while(rr-ll>eps)
16         {
17             midx=(ll+ll+rr)/3;
18             midy=(ll+rr+rr)/3;
19             if(solve(midx)>solve(midy))
20             rr=midy;
21             else ll=midx;
22         }
23         if(solve(ll)<y)
24             printf("yes\n");
25         else printf("no\n");
26     }
27     return 0;
28 }
复制代码

 

posted @   Angel_Kitty  阅读(366)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示
西雅图
14:14发布
西雅图
14:14发布
6°
西南风
5级
空气质量
相对湿度
93%
今天
中雨
3°/9°
周日
雨夹雪
3°/6°
周一
小雨
3°/10°