HDU2298 Toxophily

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

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
3 0.222018 23.901887 121.909183 39.096669 110.210922 20.270030 138.355025 2028.716904 25.079551
 
Sample Output
1.561582 -1 -1
 
 
正解:三分
解题报告:
  居然是我写的第一道三分的题目,我好菜啊...
  根据高一物理知识,高度关于角度显然是一个单峰函数,那么我们可以通过三分来得到最高高度,如果最高高度都达不到y那么就是-1.(要用到一点高一的物理...)
  接着我们可以在我们取到最高高度的角度和0之间二分,得到最接近y的角度.
  总结起来就是一句话:二分用于有单调性的函数或者区间,三分用于单峰函数求最值
 
 
 1 //It is made by ljh2000
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 const int inf = (1<<30);
16 const double pi = acos(-1);
17 const double eps = 1e-8;
18 const double g = 9.80;
19 double X,Y,V,L,ans;
20 
21 inline int getint()
22 {
23     int w=0,q=0; char c=getchar();
24     while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
25     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
26 }
27 
28 inline double cal(double o){
29     double vx,vy; vx=V*cos(o); vy=V*sin(o);
30     double tim=X/vx; double H=vy*tim-g*tim*tim/2.0;
31     return H;
32 }
33 
34 inline void three_div(){
35     double l=0,r=pi/2.0,mm,mid;
36     while(r-l>eps) {
37     mm=l+(r-l)/3; mid=mm+(r-l)/3;
38     if(cal(mm)>cal(mid)) r=mid;
39     else l=mm;
40     }
41     L=l;
42 }
43 
44 inline void work(){
45     int T=getint(); double l,r,mid;
46     while(T--) {
47     cin>>X>>Y>>V; three_div();    
48     if(cal(L)<Y) { printf("-1
"); continue; }
49     if(fabs(cal(L)-Y)<=eps) { printf("%.6lf
",L); continue; }
50     l=0; r=L;
51     while(r-l>eps) {
52         mid=(l+r)*0.5;
53         if(cal(mid)<Y) l=mid,ans=mid;
54         else r=mid;
55     }
56     printf("%.6lf
",ans);
57     }
58 }
59 
60 int main()
61 {
62     work();
63     return 0;
64 }
原文地址:https://www.cnblogs.com/ljh2000-jump/p/6018465.html