ZOJ 4041 Chasing【方程||三分】

Chasing

Time Limit: 1 Second Memory Limit: 65536 KB

In a Descartes coordinate system, areas where belongs to Shinnippori, while areas where belongs to Nippori.

Van, now located at point whose position is , is found by Billy whose location is point , more specifically . It is guaranteed that and are both in Nippori, in another word, .

Van is afraid of having another wrestle with Billy, so he is trying his best to run to Shinnippori. However, Billy wants to catch him. It is known that Billy’s speed is times of Van’s. Please answer, if Billy and Van both take the best strategy, whether Billy is always able to catch Van in Nippori.

One thing to be mentioned is that Van cannot choose to stay in Nippori forever.

Input

There are multiple test cases. The first line of the input contains an integer (), indicating the number of cases.

For the following lines, each line contains five real numbers , , , , (, , ), whose meanings are stated above.

Output

For each test case output one line. If Billy is always able to catch Van in Nippori (in another word, Van never has a chance to run into Shinnippori before getting caught) output “Y” (without quotes); Otherwise output “N” (without quotes).

Sample Input
3
-2 3 -1 2 0.988
-4 -3 -6 8 4
-10 4 -5 4 2.3333

Sample Output
N
Y
Y

Hint

For the first sample case, if Van run along the line which is perpendicular to , Billy can never catch him.

Author: WANG, Yuhan Source: ZOJ Monthly, June 2018

画图可知,小人的方案是一个凸函数,那么就可以使用三分来搜索答案,至于为什么三分模板会被卡掉,还在思考。
利用方程判断是否有解是非常稳妥的做法。

#include<bits/stdc++.h>

using namespace std;
const double EPS=1e-8;
double x1,x2,y,y2,k;
double MIN=-1e6,MAX=1e6;
double cal(double x){
    double s2=sqrt((x2)*(x2)+(y2-x)*(y2-x));
    double s1=sqrt((x1)*(x1)+(y-x)*(y-x));
    return s2/s1;
}
void solve()
{
    double Left, Right;
    double mid, midmid;
    double mid_value, midmid_value;
    Left = MIN; Right = MAX;
    while (Left + EPS <= Right)
    {
        mid = Left+(Right-Left)/3.0;
        midmid = Right-(Right-Left)/3.0;
        if (cal(mid)>=cal(midmid))

            Right = midmid;
        else Left = mid;
    }
    if((cal(mid))>=k){
        printf("N\n");
    }
    else
        printf("Y\n");
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lf%lf%lf%lf%lf",&x1,&y,&x2,&y2,&k);
        if(k<1.0){
            printf("N\n");
            continue;
        }
        solve();    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/irish_moonshine/article/details/81141703
ZOJ