HDU 2298 Toxophily

来源:互联网 发布:侠客风云传 招式数据 编辑:程序博客网 时间:2024/05/16 14:29

Toxophily

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


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
 

Source
The 4th Baidu Cup final
解题思路:给你一个二维平面上的点的坐标和一支箭的初速度,求箭与x轴正向的最小交角θ使得箭的轨迹经过给定点,两种做法,一是根据数学公式解一元二次方程,二是根据交角θ与箭的射高成凸函数关系,利用三分求x=x0的射高最大时的交角θmax,再二分0到θmax求得最大的交角θ,就是答案
首先是数学解法
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <set>#include <map>#include <list>#include <queue>#include <stack>#include <deque>#include <vector>#include <bitset>#include <cmath>#include <utility>#define Maxn 100005#define Maxm 1000005#define lowbit(x) x&(-x)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define PI acos(-1.0)#define make_pair MP#define LL long long #define Inf (1LL<<62)#define inf 0x3f3f3f3f#define g 9.8#define eps 1e-8#define re freopen("in.txt","r",stdin)#define wr freopen("out.txt","w",stdout)using namespace std;int main(){    int t;    double x,y,v;    //re;wr;    scanf("%d",&t);    while(t--)    {        scanf("%lf%lf%lf",&x,&y,&v);        double a=g*x*x;        double b=-2*v*v*x;        double c=g*x*x+2*v*v*y;        double k=b*b-4*a*c;        if(k<0)        {            puts("-1");            continue;        }        double tmp1=(-b+sqrt(k))/(2*a);        double ans;        if(tmp1>0)        {            double ans1=atan(tmp1);            ans=ans1;        }        double tmp2=(-b-sqrt(k))/(2*a);        if(tmp2>0)        {            double ans2=atan(tmp2);            ans=min(ans,ans2);        }        printf("%.6lf\n",ans);    }    return 0;}

然后是三分+二分的解法
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <set>#include <map>#include <list>#include <queue>#include <stack>#include <deque>#include <vector>#include <bitset>#include <cmath>#include <utility>#define Maxn 100005#define Maxm 1000005#define lowbit(x) x&(-x)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define PI acos(-1.0)#define make_pair MP#define LL long long #define Inf (1LL<<62)#define inf 0x3f3f3f3f#define g 9.8#define eps 1e-8#define re freopen("in.txt","r",stdin)#define wr freopen("out.txt","w",stdout)using namespace std;double x,y,v;double cal(double a){    double vx=v*cos(a);    double vy=v*sin(a);    double t=x/vx;    double h=vy*t-g*t*t/2;    return h;}double tridiv(){    double l=0.0,r=PI/2,ans;    while(r-l>eps)    {        double lm=(l*2.0+r)/3.0;        double rm=(l+r*2.0)/3.0;        if(cal(lm)>cal(rm))            r=rm;        else        {            l=lm;            ans=l;        }    }    return ans;}double binary_search(double a){    double l=0.0,r=a,ans;    while(r-l>eps)    {        double m=(l+r)/2;        if(cal(m)<y)        {            l=m;            ans=l;        }        else            r=m;    }    return ans;}int main(){    int t;    //re;wr;    scanf("%d",&t);    while(t--)    {        scanf("%lf%lf%lf",&x,&y,&v);        double a=tridiv();        double maxh=cal(a);        if(maxh<y)        {            puts("-1");            continue;        }        printf("%.6lf\n",binary_search(a));    }    return 0;}


 

0 0
原创粉丝点击