首页 > ACM题库 > HDU-杭电 > hdu 4024-dwarven sniper’s hunting-分治-[解题报告]hoj
2015
04-15

hdu 4024-dwarven sniper’s hunting-分治-[解题报告]hoj

Dwarven Sniper’s hunting


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
 


Source
 


Recommend
lcy   |   We have carefully selected several similar problems for you:  4022 4021 4023 4025 4030 
 

题目大意:

D追杀L,D是一个远程英雄,D可以发出距离为L的技能,已知L这个英雄的起始位置为X1,Y1,移动速度方向矢量是LX,LY,D的起始位置为X2,Y2,D的移动速度是VD,D发出L距离的弓箭的移动速度是VB,(Lx*Lx+Ly*Ly) < vD*vD < vB*vB,问你在D充分表现自己射击能力的情况下,最少多长时间杀死L?

解题思路:

因为:(Lx*Lx+Ly*Ly) < vD*vD < vB*vB,也就是说:L的移动速度<D的移动速度<弓箭的移动速度。也就是D表现自己的能力的话,一定射击的最长距离为L,那么时间怎么算,可以二分出答案。


解题代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

// X1, Y1, X2, Y2, Lx, Ly, vD , vB and L

const double eps=1e-7;
double X1,Y1,X2,Y2,lx,ly,vd,vb,L;

double getdis(double ax,double ay,double bx,double by){
    return sqrt ( (ax-bx)*(ax-bx)+(ay-by)*(ay-by) );
}

double getmin(double ans1,double ans2){
    if(ans1<-eps) return ans2;
    if(ans2<-eps) return ans1;
    return min(ans1,ans2);
}

void solve(){
    double l=L/vb,r=1e9;
    while(r-l>eps){
        double mid=(l+r)/2;
        double X0=X1+mid*lx,Y0=Y1+mid*ly;
        double dis0=getdis(X0,Y0,X2,Y2);
        double disd=vd*(mid-L/vb);
        if(dis0<=L){
            if(disd+dis0<=L) l=mid;
            else r=mid;
        }else{
            if(disd+L>=dis0) r=mid;
            else l=mid;
        }
    }
    printf("%.3lf %.3lf\n",L,(r+l)/2);
}

int main(){
    while(cin>>X1>>Y1>>X2>>Y2>>lx>>ly>>vd>>vb>>L){
        if( fabs(X1)<eps && fabs(X2)<eps && fabs(Y1)<eps && fabs(Y2)<eps ){
            if( fabs(lx)<eps && fabs(ly)<eps && fabs(vd)<eps && fabs(vb)<eps && fabs(L)<eps ) break;
        }
        solve();
    }
    return 0;
}

版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。


  1. 有限自动机在ACM中是必须掌握的算法,实际上在面试当中几乎不可能让你单独的去实现这个算法,如果有题目要用到有限自动机来降低时间复杂度,那么这种面试题应该属于很难的级别了。

  2. 老实说,这种方法就是穷举,复杂度是2^n,之所以能够AC是应为题目的测试数据有问题,要么数据量很小,要么能够得到k == t,否则即使n = 30,也要很久才能得出结果,本人亲测

  3. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?