您的位置:首页 > 其它

搜索算法3之1005

2016-04-15 20:26 323 查看
1 题目编号:1005

2 题目内容:

Problem Description

Mr. West bought a new car! So he is travelling around the city.<br><br>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.<br><br>Can Mr. West
go across the corner?<br><img src=../../../data/images/2438-1.jpg><br>

 

Input

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

 

Output

If he can go across the corner, print "yes". Print "no" otherwise.<br>

 

Sample Input

10 6 13.5 4<br>10 6 14.5 4<br>

 

Sample Output

yes<br>no<br>

 

Source

2008 Asia Harbin Regional Contest Online

3 解题思路形成过程:给定X, Y, l, d判断是否能够拐弯。首先当X或者Y小于d,那么一定不能。其次随着角度θ的增大,最大高度h先增长后减小,即为凸函数,可以用三分法来求解。

4 感想:通过这一个题,我的思路又得到了开阔,知道了二分查找与三分搜索的各自适用范围,二分查找其本身仅适用于类似单调函数一类的问题,而如果是凸函数或凹函数时,就应该考虑三分法了;三分搜索法的思路就是若在[a,b]范围内搜索,则与二分查找法类似先取二者中间值为mid,再在[mid,b]之间取中间值为mmid(或再在[a,mid]之间取中间值为mmid亦可),通过比较f(mid)与f(mmid)的大小来缩小范围,剩余部分与二分法相同。

5 代码:

#include <iostream>

#include <cstring>

#include <cstdio>

#include <cmath>

using namespace std;

const double pi = 3.1415;

const double eps = 1e-4;

double l, x, y, w;

double calu(double a){
return l*cos(a) + (w - x*cos(a)) / sin(a);

}

double ternary_search(double l, double r){
double L, R;
while (r - l>eps){
L = (2 * l + r) / 3;
R = (2 * r + l) / 3;
if (calu(L)>calu(R))
r = R;
else
l = L;
}
return r;

}

int main()

{
while (cin >> x >> y >> l >> w){
double l = 0, r = pi / 2;
double tmp = ternary_search(l, r);
if (calu(tmp) <= y)
puts("yes");
else
puts("no");
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  搜索算法