Catching Dogs
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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) 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
一个人在坐标轴原点的位置,他有n条狗需要以速度V追回,每条狗都有自己的速度,并且这个人只有追到i,才可以追i+1,,每条狗都有自己的速度,速度为负数说明狗往坐标轴负方向跑,正则为正方向,问这个人追到所有的狗需要的时间,
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<iostream> #define INF 0x3f3f3f3f #define E 1e-6 using namespace std; int main() { int n; double v; while(scanf("%d%lf",&n,&v)!=EOF) { double t=0,tt; double x[110],y[110],xx=0; for(int i=0;i<n;i++) { scanf("%lf%lf",&x[i],&y[i]); } int flag=0; for(int i=0;i<n;i++) { x[i]+=t*y[i]; if(x[i]==xx) continue; else { if(y[i]<0) { if(xx-x[i]>E) { if(fabs(y[i])>=v) { flag=1; break; } else { tt=fabs(xx-x[i])/fabs(v+y[i]); t+=tt; xx-=tt*v; } } else { tt=fabs(xx-x[i])/fabs(v-y[i]); t+=tt; xx+=tt*v; } } else { if(xx-x[i]>E) { tt=fabs(xx-x[i])/fabs(v+y[i]); t+=tt; xx-=tt*v; } else { if(y[i]>=v) { flag=1; break; } else { tt=fabs(xx-x[i])/fabs(v-y[i]); t+=tt; xx+=tt*v; } } } } } if(flag) printf("Bad Dog\n"); else printf("%.2lf\n",t); } return 0; }