Problem I: Catching Dogs
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1140 Solved: 298
[ 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
//:题意:输入n,v,表示有n条狗,小明的速度是v,接下来输入n对数,xi,vi,表示相应的狗的初始位置和速度(vi小于0的话表示狗向左跑,大于0表示向右跑),
小明有n条狗,现在他们都在数轴上以相应速度的相应的方向跑,现在小明想捉住他们,问需要时间是多少?
//思路:
一个一个模拟就行了,没啥说的
#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; }