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) 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
#include<iostream> #include<stdio.h> #include<math.h> using namespace std; struct Node { double pos; double v; }dog[15]; double cal(double v,double dogv) { if(v>0&&dogv>0) { if(v>dogv) return v-dogv; else return 0; } if(v<0&&dogv<0) { if(v<dogv) return dogv-v; else return 0; } if(v*dogv<=0) return fabs(v+0.0)+fabs(dogv); } int main() { int n; double v; while(scanf("%d%lf",&n,&v)!=EOF) { if(n<=0) continue; for(int i=0;i<n;i++) { scanf("%lf%lf",&dog[i].pos,&dog[i].v); } double nt=0; double ans=0; double posp=0; bool flag=true; for(int i=0;i<n;i++) { dog[i].pos=dog[i].pos+(dog[i].v*ans); if(fabs(posp-dog[i].pos)<1e-6) continue; if(posp>dog[i].pos) v=-fabs(v); if(posp<dog[i].pos) v=fabs(v); double catv=cal(v,dog[i].v); if(catv==0) {printf("Bad Dog ");flag=false;break;} double catx=posp-dog[i].pos; // cout<<catx<<"**"<<catv<<endl; nt=catx/catv; posp=fabs(nt)*v+posp; // cout<<"posp**"<<posp<<endl; ans+=fabs(nt); } if(flag) printf("%.2lf ",ans); } return 0; }
模拟题,水题。
考虑dogv=0的这种情况。