搜索算法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;
}
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;
}
相关文章推荐
- 搜索0之1001
- 搜索算法1之1002
- 搜索算法2之1008
- VC++源码分析 - 中国象棋源码分析
- 使用栈和队列实现迷宫路径查找算法
- ‘聪明的搜索算法’ A*算法
- 2004上半年软设上午试题第64、65类比二分搜索算法
- Cpp环境【GRYZ2015】【Vijos3150】 足球联赛
- 搜索算法(一) 2018-2-28
- IDA*对A*的改进
- Jump Search-跳跃搜索算法
- 从零开始学算法(三)搜索算法(一)(深度优先搜索)之地图迷宫
- alpha-beta剪枝法
- 算24点(DFS)
- MITx - 6.00.1x 笔记(6) Algorithmic Complexity
- MOOC人工智能原理学习笔记3——无信息搜索
- 【BZOJ3990】【SDOI2015】排序
- 搜索算法14之总结
- 搜索算法13之1012
- 搜索算法12之1015