HDU 4024 Dwarven Sniper’s hunting(二分)

Dwarven Sniper’s hunting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 866 Accepted Submission(s): 437

Problem Description
Now the hunting starts in the world named DOTA, a stupid PC game which cannot play with others together.
Among the individuals in the game, there are two heroes named Dwarven Sniper and Lycanthrope. Lycanthrope wants to escape from being captured; however, our Dwarven Sniper won’t let him go! He will use the Silver Bullet to kill the Lycanthrope by only one shot! Yes, that’s enough.
Lycanthrope is running on a line in the map with a constant speed and direction. The weapon range of the Silver Bullet is limited by L meters. Dwarven Sniper can run for a while freely, and then shoot Lycanthrope. In order to show his excellent shooting skill, Dwarven Sniper wants the Silver Bullet flying as far as possible. But don’t forget the flying time of the Silver Bullet due to considerable weight of the bullet. And Dwarven Sniper wants to stop the hunting as quickly as possible. So if there is more than one way to show his excellent skill, he would choose the fastest way. In this problem we consider the Silver Bullet and Lycanthrope as two points.
Now Dwarven Sniper wants to know the maximum length that the Silver Bullet can fly, and the shortest time that the hunting lasts. Specifically, the total hunting time is defined as the time interval from the start of hunting to the moment that the bullet hit Lycanthrope. Can you help him?

这里写图片描述

Input
There are several test cases. Each of them contains only one line which consist of 9 real numbers, that are X1, Y1, X2, Y2, Lx, Ly, vD , vB and L (-10000 <= X1 , Y1 , X2 , Y2 , Lx , Ly <= 10000 , 0 <= vD , vB , L <=100000).
The pair (X1, Y1) is the starting position of the Lycanthrope while (X2, Y2) is the starting position of Dwarven Sniper.
(Lx, Ly) is the moving vector per second of the Lycanthrope.
vD is the speed of the Dwarven Sniper .
vB is the speed of the Silver Bullet.
All units are meters/second.
It is guaranteed that (Lx*Lx+Ly*Ly) < vD*vD < vB*vB , and Dwarven Sniper’s starting position is different from Lycanthrope’s position. The input ends with a line containing all zeros.

Output
For each test case, output two real numbers S and T in a line separated by a single space denoting that the Silver bullet flies S meters before hitting Lycanthrope and the hunting lasts for T seconds, both with 3 digits after the decimal point.
You may assume that Dwarven Sniper can finish his hunting within no more than 1e+9 seconds.

Sample Input
-1 0 0 10 1 0 2 10 10
0 0 0 5 0 1 2 6 6
0 0 0 0 0 0 0 0 0

Sample Output
10.000 1.000
6.000 3.000

题意看了大半天,读题半小时,写题五分钟的题

题意

一个人要去追另一个人,给你两个人的初始坐标,被追的人的初始坐标 x 1 , y 1 ,追的人的坐标是 x 2 , y 2 ,再给你被追的人的运动向量即 L x , L y 每秒在x方向和y方向运动几米,再给你追的人的运动速度 v d ,和他射出的子弹的速度 v b ,还有一个子弹的有效半径 L ,追的人可以跑任意方向,要求是在被追的人开始到被子弹打到的时间尽可能少的情况下,子弹的飞行距离尽可能远

思路

因为题目说保证有解,所以子弹要飞的尽量远那就是极限距离L即可,也就是说我们可以二分时间,每次得到一个时间后可以得到被追的人的坐标,以这个坐标作为圆点做一个半径为L的圆,然后那么追的人要射到被追的人只用运动他的起始点到圆心的距离减去半径这段了,那么被追的人的总时间我们就可以得到了,我们比较被追的人的时间和追的人的时间,如果追的人的时间小于被追的人的时间说明我们二分的时间太大了,那么答案应该在l到mid这一段,反之在mid到r这一段
还有一点要注意的是,追的人的起始点可能在圆的内部,所以这个时候是L减去点到圆心的距离,二分函数只用加一个绝对值就可以了

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<set>
using namespace std;
double X1,Y1,x2,y2,lx,ly,vd,vb,L;
double dis(double x1,double y1,double x2,double y2)
{
    return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}
double fmid(double t)
{
    double x1=X1;
    double y1=Y1;
    x1+=t*lx;
    y1+=t*ly;
    return fabs(dis(x1,y1,x2,y2)-L)/vd+L/vb;
}
int main()
{
     while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf",&X1,&Y1,&x2,&y2,&lx,&ly,&vd,&vb,&L)!=EOF)
    {
        if(X1==0&&Y1==0&&x2==0&&y2==0&&lx==0&&ly==0&&vd==0&&vb==0&&L==0)
            break;
    double l=0;
    double r=1e9;
    while(fabs(r-l)>1e-8)
    {
        double mid=(l+r)/2;
        if(fmid(mid)<mid)
        {
            r=mid;
        }
        else
            l=mid;
    }
    printf("%.3lf %.3lf\n",L,l);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ftx456789/article/details/79853785