Problem I: Catching Dogs

Problem I: Catching Dogs

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 0   Solved: 0

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) andv(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

解题思路:Diao Yang抓狗,因为已经固定了抓狗的顺序,即必须先抓到第i只,才能抓第(i+1)只,所以我们只需要一只一只处理即可,但是不能忘了更新Diao Yang的位置,有以下几种情况需要考虑:

①当Diao Yang和狗同向时,若Diao Yang的速度小于等于狗的速度,且Diao Yang的当前位置与狗的当前位置不同时,这种是永远追不上狗的,故输出"Bad Dog"即可;

当Diao Yang和狗相向时,这种情况不管Diao Yang和狗的速度多少,都可以抓到

③而当Diao Yang的当前位置与狗的当前位置相同时,抓住当前狗是不需要时间的

总而言之,此题考查的就是我们做题是否细心

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 2005;
const int M = 40;
const int inf = 100000000;
const int mod = 2009;
struct node
{
    int v;
    double a;
    node(){}
    node(double a0,int v0):a(a0),v(v0){}
};
queue<node> q;
int main()
{
    int n,v,i,b;
    double p,t,t1,a;
    bool flag;
    node u;
    while(~scanf("%d%d",&n,&v))
    {
        p=0,t=0;flag=true;
        while(!q.empty())
            q.pop();
        for(i=0;i<n;i++)
        {
            scanf("%lf%d",&a,&b);
            q.push(node(a,b));
        }
        while(!q.empty())
        {
            u=q.front();
            q.pop();
            u.a+=u.v*t;
            if(u.a<p)
                v=-abs(v);
            else
                v=abs(v);
            if(u.a!=p&&u.v*v>0.0&&abs(u.v)>=abs(v))
            {
                flag=false; 
                puts("Bad Dog");
                break;  
            }
            if(u.a==p)
                continue;
            t1=(u.a-p)/(v-u.v);
            p=u.a+u.v*t1;
            t+=t1;
            //printf("%.2f %.2f %.2f\n",t1,t,p);
        }
        if(flag)
            printf("%.2f\n",t);
    }
    return 0;
}
 
/**************************************************************
    Problem: 20
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1508 kb
****************************************************************/

菜鸟成长记

你可能感兴趣的:(ACM)