Problem I: Catching Dogs 几何水题

Problem I: Catching Dogs

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1145  Solved: 300
[Submit][Status][Web Board]
Description

   Diao Yang keeps many dogs. But today his dogs all run away. Diao Yang has to catch them. To simplify the problem, we assume Diao Yang and all the dogs are on a number axis. The dogs are numbered from 1 to n. At first Diao Yang is on the original point and his speed is v. The ith dog is on the point ai and its speed is vi . Diao Yang will catch the dog by the order of their numbers. Which means only if Diao Yang has caught the ith dog, he can start to catch the (i+1)th dog, and immediately. Note that When Diao Yang catching a dog, he will run toward the dog and he will never stop or change his direction until he has caught the dog. Now Diao Yang wants to know how long it takes for him to catch all the dogs.
Input

    There are multiple test cases. In each test case, the first line contains two positive integers n(n≤10) and v(1≤v≤10). Then n lines followed, each line contains two integers ai(|ai|≤50) and vi(|vi|≤5). vi<0 means the dog runs toward left and vi>0 means the dog runs toward right. The input will end by EOF.
Output

    For each test case, output the answer. The answer should be rounded to 2 digits after decimal point. If Diao Yang cannot catch all the dogs, output “Bad Dog”(without quotes).
Sample Input

2 5
-2 -3
2 3
1 6
2 -2
1 1
0 -1
1 1
-1 -1
Sample Output

6.00
0.25
0.00
Bad Dog
HINT




///简单的几何水题 分情况讨论即可///注意int输入double输出时尤其注意!!
来源: HZAU
#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <cstring>
#include <set>
using namespace std;
typedef struct {double a,v;}DOGP;///用int存放时WA!
DOGP Point[20];
double Handle(int N,int V)
{
    for(int i=1;i<=N;i++) scanf("%lf %lf",&Point[i].a,&Point[i].v);
    double P=0,time=0,Dt,Dx,Dv;
    for(int i=1;i<=N;i++)
    {
        Point[i].a +=(time * Point[i].v);
        if(P < Point[i].a)  ///在狗i左侧
        {
            Dx = Point[i].a - P; ///距离Dx
            Dv = V - Point[i].v; ///相对速度
            if(Dv <= 0) return -1; ///追不上
            Dt = (double)Dx/(double)Dv;
            P+=((double)V*Dt);
            time+=Dt;
        }
        else if(P == Point[i].a) continue;///就在同一位置
        else ///在狗右侧
        {
            Dx = P - Point[i].a;
            Dv = V + Point[i].v;
            if(Dv <= 0) return -1;
            Dt = Dx/Dv;
            P-=((double)V*Dt);
            time+=Dt;
        }
    }
    return time;
}
int main()
{
    //freopen("D:\\test.txt","r",stdin);
    //freopen("D:\\tzzt.out","w",stdout);
    int n,v;
    while(~scanf("%d %d",&n,&v))
    {
        double ans = Handle(n,v);
        if(ans>=0) printf("%.2lf\n",ans);
        else printf("Bad Dog\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值