HDU 2298 三分

斜抛从(0,0)到(x,y),问其角度。

首先观察下就知道抛物线上横坐标为x的点与给定的点的距离与角度关系并不是线性的,当角度大于一定值时可能会时距离单调递减,所以先三分求个角度范围,保证其点一定在抛物线下方,这样距离和角度的关系就是单调的了,再二分角度即可。

 

/** @Date    : 2017-09-23 23:17:11
  * @FileName: HDU 2298 三分.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$ 
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
const double Pi = acos(-1.0);
const double g = 9.8;

double check(double agl, double x, double v)
{
	if(x == 0 && agl - Pi / 2.00 < eps)
		return v * v / 2.000 / g;
	double va = v * sin(agl);
	double vb = v * cos(agl);
	double t = x / vb;
	double y = va * t - g * t * t / 2;
	return y;
}
int main()
{
	int T;
	cin >> T;
	while(T--)
	{
		double x, y, v;
		scanf("%lf%lf%lf", &x, &y, &v);
		if(x == 0)
		{
			double ny = check(Pi/2.00, 0, v);
			if(y - ny > eps)
				printf("-1\n");
			else 
				printf("%.6lf\n", Pi/2.00);
			continue;
		}
		double l = 0;
		double r = Pi / 2.0;
		while(r - l > eps)
		{
			double lmid = (l + l + r) / 3.0;
			double rmid = (l + r + r) / 3.0;
			if(check(lmid, x, v) > check(rmid, x, v))//三分一个最大角度范围使点总在曲线下方
				r = rmid;
			else l = lmid;
		}
		if(y - check(l, x, v) > eps)
		{
			printf("-1\n");
			continue;
		}
		double ll = 0;
		double rr = l;
		while(rr - ll > eps)
		{
			double mid = (ll + rr) / 2.0;
			if(check(mid, x, v) - y > eps)
				rr = mid;
			else
				ll = mid;
		}
		printf("%.6lf\n", ll);
	}
    return 0;
}
posted @ 2017-09-24 00:38  Lweleth  阅读(194)  评论(0编辑  收藏  举报