题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3492
Prototype is a 3D game which allow you to control a person named Alex with much super ability to finish missions with gut along. Alex has the abilitiy to glide in the sky. What's more, he can make at most 3-level glide, which means before he lands at the ground, he has two chances to adjust and perform another glide. We assume that each time he perform a glide, his vertical speed become zero and glide forward with a new speed. And the orbit will be a parabola due to the gravity.
To make the problem easier, we now only consider at most 2-level glide. The binomial coefficient of the mathematical equation of the fist glide will be given as -a and the second will be -b, which means the formulations are (y - y0) = -ax2 and (y - y0) = -b(x - x0)2. As the picture above, Alex perform a glide from the top of Building1, make a 1-level or a 2-level glide and lands exactly at point B. What's more, there is Building2 standing between Building1 and point B. Alex has to avoid crashing onto it.
Input
There are no more than 15 cases. Proceed till the end of file.
Each case contains only one line of six real number h1, h2, d1, d2, a, b. h1 is the height of Building1, h2 is the height of Building2, d1 is the X-distance between Building1 and Building2, d2 is the X-distance between point B and Building1. These four numbers are in [0, 1000] , and satisfies d1 < d2. And a and b are in (0, 1000].
Output
If it is possible for Alex to land exactly on point B, print Yes, otherwise print No.
Sample Input
25 1 6 7 1 1 4 3 1 2 1 1
Sample Output
Yes Yes
HINT
In case 2, Alex just glide over the building2 and do not crash onto it.
个人觉得这道题目的主要难点在于审题。由于题目配图比较简略,一开始没搞清楚两个公式之间的关系。事实上式1和式2的x0y0是没有关系的。用比较清晰的写法的话式1和式2应该写成(y1 - y0') = -ax12 和 (y2 - y0") = -b(x2 - x0")2。
为了看起来更清晰直观,把(x0',y0')替换成(0,h1),把(x0",y0")替换成(x0,y0)。那么原式1式2 就变成
(y - h1) = -ax2 (1)
(y - y0) = -b(x - x0)2
接下来就是求出两条抛物线交点A的位置。
由于B点已知,将其带入式2则有-y0=-b(d2-x0)^2 (2)
将交点A的坐标(x0,y0)代入(1),有
y0 - h1 = -ax0^2 (3)
联立(2)(3)得
(a+b)x0^2 - 2bd2x0 - h1 + bd2^2 = 0
根据求根公式
del = (2bd2)^2 - 4(a+b)(bd2^2 - h1)
x1 = (2bd2 + del )/2(a+b), x2 = (2bd2 - del )/2(a+b)
将d1代入方程求出对应的高度是否>= h2。
实际提交的时候一直WA,最后发现是因为计算精度不够。
附AC源码:
#include <iostream>
#include <cmath>
using namespace std;
double h1, h2, d1, d2, a, b;
bool check(double x0, double y0)
{
double d;
if (x0 < d1)
{
d = y0 - b * (d1 - x0) * (d1 - x0);
}
else
{
d = h1 - a * d1 * d1;
}
if (d >= h2) return true;
return false;
}
int main ()
{
while (cin >> h1 >> h2 >> d1 >> d2 >> a >> b )
{
bool ans = false;
double del = (-2. * b * d2) * (-2. * b * d2) - 4 * (a + b) * (b * d2 * d2 - h1);
if (del >= 0)
{
del = sqrt(fabs(del));
double x1, x2, y1, y2;
x1 = (2 * b * d2 + del ) / (2 * (a + b));
x2 = (2 * b * d2 - del ) / (2 * (a + b));
y1 = h1 - a * x1 * x1;
y2 = h1 - a * x2 * x2;
if (x1 >= 0 && x1 <= d2)
{
ans = check(x1,y1);
}
if (x2 >= 0 && x2 <= d2)
{
ans = check(x2,y2);
}
}
if(ans) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
一直WA的关键在于计算del的时候要用-2.(double)不能用-2(int)