1. 程式人生 > >HDU-2438-Turn the corner(三分+思維)

HDU-2438-Turn the corner(三分+思維)

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


 思路:

三分題關鍵是要確定決定的結果的因變數f(x),和決定f(x)的自變數x。

在這個題中,老司機拐彎的時候,最佳拐彎路徑是右側兩個車角緊貼著街邊行駛(如下圖所示)。

這時候就要做一條輔助線PH,來時車道的左側延長線,交車左邊於P,右邊於Q,交有側車道沿於H。

我們會發現,當max(PH)>Y時車是不能拐過去的,會頂到拐角處。否則就可以過去。

這樣顯而易見,我們的f(x)就是PH的長度。而x我選用了角d,用三角函式分別算出PQ和QH的長度他們的和就是所求PH長。

f(d)=(x-l*sin(d))*tan(d)+(w-(x-l*sin(d))/cos(d))/sin(d)

然後就是常規三分了。    

 

 

 


 

#include<iostream>
#include<cstdio>
#include<cmath>
#define pi 3.1415926
using namespace std;
double x,y,l,w;

double f(double d){ return (x-l*sin(d))*tan(d)+(w-(x-l*sin(d))/cos(d))/sin(d); } int main(){ while(~scanf("%lf%lf%lf%lf",&x,&y,&l,&w)){ double low=0,high=pi/2.0; while(high-low>=0.0001){ double temp=(high-low)/3.0; double lm=low+temp; double rm=high-temp; if(f(lm)>f(rm)) high=rm; else low=lm; } if(f(low)>y) cout<<"no"<<endl; else cout<<"yes"<<endl; } return 0; }