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 ithdog 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.
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.
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).
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
double n,v;
struct node
{
double pos;
double v;
}N[105];
double init=0;
double tim(double target,double vv)
{
double dis,vvv;
if(init>target)
{
dis=init-target;
vvv=v+vv;
}
else
{
dis=target-init;
vvv=v-vv;
}
if(dis==0)
return 0;
if(vvv<=0)
return -1;
return dis*1.0/vvv*1.0;
}
int main()
{
while(scanf("%lf %lf",&n,&v)!=EOF)
{
memset(N,0,sizeof(N));
int flag=0;
for(int i=1;i<=n;i++)
scanf("%lf %lf",&N[i].pos,&N[i].v);
double t=0,tt=0;;
init=0;
for(int i=1;i<=n;i++)
{
t=tim(N[i].pos+tt*N[i].v,N[i].v);
if(t<0)
{
flag=1;
break;
}
if(t>0)
tt+=t;
init=N[i].pos+N[i].v*tt;
}
if(flag)
cout<<"Bad Dog"<<endl;
else
printf("%.2f\n",tt);
}
return 0;
}