NPY and shot
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 772 Accepted Submission(s): 316
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(T≤10000)
,which indicates the number of test cases.
The next T lines,each contains 2 integers H(0≤h≤10000m) ,which means the height of NPY,and v0(0≤v0≤10000m/s) , which means the initial velocity.
The next T lines,each contains 2 integers H(0≤h≤10000m) ,which means the height of NPY,and v0(0≤v0≤10000m/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.99HintIf the height of NPY is 0,and he throws the shot at the 45° angle, he can throw farthest.
Source
题意:从高度H以0到90度之间的某个角度给定的速度V扔出,求最远距离
因为求的是极值,符合单峰性质(?),所以用三分来做
如果函数f(x)在区间[a, b]上只有唯一的最大值点(或最小值点)C,而在最大值点(或最小值点)C的左侧,函数单调增加(减少);在点C的右侧,函数单调减少(增加),则称这个函数为区间[a, b]上的
单峰函数
.
如果函数f(x)在区间(a, b)上有唯一的极值点,则f(x)在区间[a, b]上是单峰函数.
不过物理公式推了半天才推出来,真是醉了
代码:
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<vector>
#define F first
#define S second
#define PI acos(-1.0)
#define E exp(1.0)
#define INF 0xFFFFFFF
#define MAX -INF
#define eps 1e-8
#define len(a) (__int64)strlen(a)
#define mem0(a) (memset(a,0,sizeof(a)))
#define mem1(a) (memset(a,-1,sizeof(a)))
using namespace std;
__int64 gcd(__int64 a, __int64 b) {
return b ? gcd(b, a % b) : a;
}
__int64 lcm(__int64 a, __int64 b) {
return a / gcd(a, b) * b;
}
__int64 max(__int64 a, __int64 b) {
return a > b ? a : b;
}
__int64 min(__int64 a, __int64 b) {
return a < b ? a : b;
}
double h,v,g=9.8;
double f(double theta)
{
double t=v*sin(theta)/g+sqrt(v*v*sin(theta)*sin(theta)/g/g+2.0*h/g);
return t*v*cos(theta);
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int T;
double l,r,mid,mmid;
scanf("%d",&T);
while (T--) {
scanf("%lf%lf",&h,&v);
l=0;
r=PI/2.0;
while(r-l>=eps)
{
mid=(l+r)/2.0;
mmid=(mid+r)/2;
if(f(mid)<f(mmid))
l=mid;
else r=mmid;
}
printf("%.2lf\n",f(l));
}
}