hdu 2298 - Toxophily [二分]

Toxophily

               Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1440    Accepted Submission(s): 749

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

题意:这个题题意似乎好理解,给你一个向上速度V,让你求出最小的角度使得你射出的箭能够到达给定的点(x,y)。如果不能输出为-1,如果能到达,输出最小的角度。

分析:首先我们建立物理模型,如果射的箭能够达到点(x,y)且到达的角度最小,那么到达这个点的竖直方向上的速度必然是0,或者说是(vy-0)/9.8 < t 时,这个解必定不是最小的,这是首先我们要知道的。然后直接二分角度求解就OK了。

#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-10;
int T;
double x,y,v,ans;
void Search() {
    double mid,l,r,vx,vy,t,mVal;
    l = 0;r = PI/2.0;
    bool suc = false; //判断能不能到达目标点
    //二分查找能够到达该点的角度
    while(r-l>=eps)
    {
        mid = (l+r)/2.0;
        // 求出初始水平速度和初始竖直速度,以及当横坐标到达x时的时间
        vx = v*cos(mid),vy = v*sin(mid);t = x/vx;
        //求出横坐标为x时所能到达的Y值,Y>y则说明角度太大,Y<y说明角度太小
        mVal = vy*t-0.5*9.8*t*t;
        if(mVal < y)
        {
            l = mid;
        }
        else if(mVal > y || vy/9.8 < t)// vy/9.8 < t是用来判断 当前的值是不是最小的【PS:由于OJ数据问题,即便不加也不会WA】
        {
            suc = true;
            ans = mid;
            r = mid;
        }
        else
        {
            suc = true;
            ans = mid;
            break;
        }
    }
    if(suc) printf("%.6lf\n",ans);
    else printf("-1\n");
}
int main () {
    //freopen("input.in","r",stdin);
    for(scanf("%d",&T);T--;)
    {
        scanf("%lf %lf %lf",&x,&y,&v);
        Search();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 03:29:28

hdu 2298 - Toxophily [二分]的相关文章

Toxophily HDU - 2298 三分+二分

题目:https://vjudge.net/contest/364745#problem/B 先用三分求出最高点Y,然后在进行二分,求出角度 注意写法   PI的弧度是 acos(-1)/2-EPS接近90度的时候相当于除以0二分的精度可以用迭代次数来保证,比如100次 #include <iostream> #include <cmath> using namespace std; const double EPS = 1e-8; int T; double x, y, v; d

HDU -2298 Toxophily

这道题目,可以推出物理公式直接来做,但是如果退不出来就必须用程序的一种算法来实现了,物理公式只是适合这一个或者某个题,但是这种下面这种解决问题的方法确实解决了一类问题 ----三分法,大家可能都听说过二分法,没有听说三分法,确实三分法很冷,但是学会了就是学会了,而且他的计算速度并不慢,时间复杂度是log型的,所以推荐大家学会这种方法,下面是具体的代码实现,包括怎么三分的过程,不是平均分成三段,而是先分成一半,在接着把后面的一半接着再分一半: 1 #include <stdio.h> 2 #in

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

hdu 3641 数论 二分求符合条件的最小值数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*==================================================== 二分查找符合条件的最小值 ======================================================*/ ll solve() { __int64 low = 0, high = INF, mid ; while(low <=

HDU 4768 Flyer (二分)

OJ题目:click here~~ 题目分析:n个[a  b] 区间,对于i 属于[a  b]  ,从a开始,间隔c ,即i = a , i = a + c , i = a + 2*c -- 将x[ i ] 加1 ,x[ i ] 初值为0 . 已知最多只有一个x[ i ] 为奇数.找到这个i , 和这个奇数. 由于最多只有一个奇数,且奇数 + 偶数 = 奇数.用二分夹逼出这个奇数的位置.找到这个位置,再计算这个奇数就很容易了. AC_CODE const int maxn = 20002; LL

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

HDU 2295 DLX 二分

Radar Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2229    Accepted Submission(s): 888 Problem Description N cities of the Java Kingdom need to be covered by radars for being in a state of w

HDU 2298:Toxophily(推公式)

http://acm.hdu.edu.cn/showproblem.php?pid=2298 题意:给出一个x,y,v,问从(0,0)以v为初速度射箭,能否射到(x,y)这个点,如果能,输出最小的射出角度(与x轴),否则输出-1. 思路:首先考虑不能到达的情况,由动能定理mgy > 1 / 2 * m * v * v的时候,就输出-1. 然后可以列出两个式子: x = v * t * cos(θ)  ① y = v * t * sin(θ) - 1 / 2 * g * t * t. ② 把①带入