NPY and shot

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 808    Accepted Submission(s): 342


Problem Description
NPY is going to have a PE test.One of the test subjects is throwing the shot.The height of NPY is H meters.He can throw the shot at the speed of v0 m/s and at the height of exactly H meters.He wonders if he throws the shot at the best angle,how far can he throw ?(The acceleration of gravity, g, is 9.8m/s2)
 

 

Input
The first line contains a integer T(T10000),which indicates the number of test cases.
The next T lines,each contains 2 integers H(0h10000m),which means the height of NPY,and v0(0v010000m/s), which means the initial velocity.
 

 

Output
For each query,print a real number X that was rounded to 2 digits after decimal point in a separate line.X indicates the farthest distance he can throw.
 

 

Sample Input
2 0 1 1 2
 

 

Sample Output
0.10 0.99
Hint
If the height of NPY is 0,and he throws the shot at the 45° angle, he can throw farthest.
 

 

Source
 
题意:求解斜抛运动能够抛到的最远距离。
题解:设速度与水平方向成的角度为 a (0<=a<=pi/2) 我们可以知道这个 a 在这个范围内是一个单峰极值函数,所以可以用三分求解.
水平速度为 v0*cos(a) 竖直方向速度为 v0*sin(a)
假设小球落地的时间为 t = t1+t2 t1是小球到达顶点的时间,t2是小球从顶点落向地面的时间.
v0*sin(a)-g*t1 = 0 (注意加速度方向)
1/2*g*t2*t2 = h+v0*sin(a)*t1-1/2*g*t1*t1
x = v0*cos(a)*(t1+t2)
带进三分公式算就好了.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
double v,h,t1,t2;
const double g = 9.8;
const double pi = 3.1415926;
const double eps = 1e-8;
double Calc(double t)
{
    t1 = sin(t)*v/g;
    t2 = sqrt((h+v*sin(t)*t1-0.5*g*t1*t1)*2/g);
    return (t1+t2)*v*cos(t);
}
double solve(double MIN,double MAX)
{
    double Left, Right;
    double mid, midmid;
    double mid_value, midmid_value;
    Left = MIN;
    Right = MAX;
    while (Left +eps < Right)
    {
        mid = (Left + Right) / 2;
        midmid = (mid + Right) / 2;
        mid_value = Calc(mid);
        midmid_value = Calc(midmid);
        ///求最大值改成>= 最小值改成<=
        if (mid_value >= midmid_value) Right = midmid;
        else Left = mid;
    }
    return Left;
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%lf%lf",&h,&v);
        double angle = solve(0,pi/2); ///注意是[0,pi/2],不是[0,90]
        printf("%.2lf\n",Calc(angle));
    }
    return 0;
}