首页 > 学院 > 开发设计 > 正文

uva11796 Dog Distance

2019-11-06 09:15:31
字体:
来源:转载
供稿:网友

Two dogs, Ranga and Banga, are running randomly following two different paths. They both run for T seconds with different speeds. Ranga runs with a constant speed of R m/s, whereas Banga runs with a constant speed of S m/s. Both the dogs start and stop at the same time. Let D ( t ) be the distance between the two dogs at time t . The dog distance is equal to the difference between the max- imum and the minimum distance between the two dogs in their whole journey. Mathematically, Dog Distance = f max( D ( a )) 0  a  T g

如果两条狗都跑直线的话,只需要把其中一个看成不动的参考系,这样问题就变成了点到线段的距离。 拐弯也不要紧,从当前状态到某一条狗拐弯之前的一段时间还是可以用上面的方法操作,重复O(a+b)次即可。

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;const double eps=1e-8,oo=1e15;int cmp(double x){ if (x>eps) return 1; if (fabs(x)<eps) return 0; return -1;}int m,n;double mx,mn;struct vector{ double x,y; void rd() { scanf("%lf%lf",&x,&y); } bool Operator < (const vector &v1) const { return cmp(x-v1.x)==-1||(cmp(x-v1.x)==0&&cmp(y-v1.y)==-1); } bool operator == (const vector &v1) const { return cmp(x-v1.x)==0&&cmp(y-v1.y)==0; } vector operator + (const vector &v1) const { return (vector){x+v1.x,y+v1.y}; } vector operator - (const vector &v1) const { return (vector){x-v1.x,y-v1.y}; } vector operator * (const double &k) const { return (vector){x*k,y*k}; } vector operator / (const double &k) const { return (vector){x/k,y/k}; }}a[60],b[60];typedef vector point;double dot(vector v1,vector v2){ return v1.x*v2.x+v1.y*v2.y;}double cross(vector v1,vector v2){ return v1.x*v2.y-v1.y*v2.x;}double dis(point a,point b){ return sqrt(dot(b-a,b-a));}struct line{ point a; vector v;};double dis(point a,line l){ if (cmp(dot(l.v,l.v))==0) return dis(a,l.a); return fabs(cross(a-l.a,l.v))/sqrt(dot(l.v,l.v));}struct seg{ point a,b;};double dis(point a,seg s){ if (cmp(dot(a-s.a,s.b-s.a))==-1||cmp(dot(a-s.b,s.a-s.b))==-1) return min(dis(a,s.a),dis(a,s.b)); return dis(a,(line){s.a,s.b-s.a});}void upd(point pa,point pb,vector va,vector vb){ vector v=vb-va; mx=max(mx,max(dis(pa,pb),dis(pa,pb+v))); mn=min(mn,dis(pa,(seg){pb,pb+v}));}int solve(){ double la=0,lb=0,da,db,x; point pa,pb; vector va,vb; scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) a[i].rd(); for (int i=1;i<=m;i++) b[i].rd(); for (int i=1;i<n;i++) la+=dis(a[i],a[i+1]); for (int i=1;i<m;i++) lb+=dis(b[i],b[i+1]); pa=a[1]; pb=b[1]; mx=-oo,mn=oo; for (int i=1,j=1;i<n&&j<m;) { da=dis(pa,a[i+1]); db=dis(pb,b[j+1]); x=min(da/la,db/lb); va=(a[i+1]-pa)/da*x*la; vb=(b[j+1]-pb)/db*x*lb; upd(pa,pb,va,vb); pa=pa+va; if (pa==a[i+1]) i++; pb=pb+vb; if (pb==b[j+1]) j++; } return mx-mn+0.5;}int main(){ int T,K=0; scanf("%d",&T); while (T--) PRintf("Case %d: %d/n",++K,solve());}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表