HDU2298(三分法+二分法)
2013-06-04 21:42
169 查看
Toxophily
[b]Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 729 Accepted Submission(s): 427
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
3
0.222018 23.901887 121.909183
39.096669 110.210922 20.270030
138.355025 2028.716904 25.079551
[align=left]Sample Output[/align]
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; else high=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; else high=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; }
相关文章推荐
- 快速排序里的学问:从猜数字开始
- [LeetCode] Sqrt(x)
- [LeetCode] Pow(x, n)
- [LeetCode] Search Insert Position
- [LeetCode] Search for a Range
- [LeetCode] Search in Rotated Sorted Array
- PAT 1057 Stack (30)
- 区间插入求和 — 线段树入门(二)
- HDU4004The Frog's Games二分!二分!
- poj3737UmBasketella三分法求极值
- hdu2578Dating with girls(1)(二分查找或者STL map)
- hdu 2141 (二分+排序)
- hdu 2199 Can you solve this equation?
- 找出一堆硬币中较重的一个(java 算法)
- HDU4022(二分法)
- HDU1969(二分法)
- 屌丝的常用排序-----three
- python二分查找法
- 通过Flex布局实现三等分
- AS3二分查询