HDU 2298 Toxophily

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
  
  
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
 

Source
解题思路:给你一个二维平面上的点的坐标和一支箭的初速度,求箭与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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值