HDU2298(三分法+二分法)

来源:互联网 发布:秘鲁域名后缀 编辑:程序博客网 时间:2024/06/07 10:23

Toxophily

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


Problem Description
The recreation center of WHU ACM Team has indoor billiards, Ping Pang, chess and bridge, toxophily, deluxe ballrooms KTV rooms, fishing, climbing, and so on.
We all like toxophily.

Bob is hooked on toxophily recently. Assume that Bob is at point (0,0) and he wants to shoot the fruits on a nearby tree. He can adjust the angle to fix the trajectory. Unfortunately, he always fails at that. Can you help him?

Now given the object's coordinates, please calculate the angle between the arrow and x-axis at Bob's point. Assume that g=9.8N/m.
 

Input
The input consists of several test cases. The first line of input consists of an integer T, indicating the number of test cases. Each test case is on a separated line, and it consists three floating point numbers: x, y, v. x and y indicate the coordinate of the fruit. v is the arrow's exit speed.
Technical Specification

1. T ≤ 100.
2. 0 ≤ x, y, v ≤ 10000.
 

Output
For each test case, output the smallest answer rounded to six fractional digits on a separated line.
Output "-1", if there's no possible answer.

Please use radian as unit.
 

Sample Input
30.222018 23.901887 121.90918339.096669 110.210922 20.270030138.355025 2028.716904 25.079551
 

Sample Output
1.561582-1-1
//首先分析可知当水平方向位移一定时竖直位移随与x轴正方向夹角的增加而先增加后减小,是凸函数,存在极值
//用三分查找法凹函数或凸函数的(最小值)极小值或(最大值)极大值点
//然后二分法找到符合题目条件的点
#include<iostream>#include<cstdio>#include<cmath>using namespace std;const double MSM=1.0e-9;const double PI=acos(-1.0);double x,y,v;double get_y(double temp){double t=x/(cos(temp)*v);double h=v*sin(temp)*t-0.5*9.8*t*t;return h;}double three_devide(double low,double high){double l,h;while(high-low>=MSM){l=(low*2+high)/3;h=(low+high*2)/3;if(get_y(l)<=get_y(h))low=l;elsehigh=h;}return (l+h)/2;}double two_devide(double low,double high){double mid;while(high-low>=MSM){mid=(low+high)/2;if(get_y(mid)<=y)low=mid;elsehigh=mid;}return mid;}int main(){int t;cin>>t;while(t--){scanf("%lf%lf%lf",&x,&y,&v);double temp=three_devide(0,PI/2);if(get_y(temp)<y)printf("-1\n");else printf("%.6lf\n",two_devide(0,temp));}return 0;}

原创粉丝点击