计算几何:
题解转自:点击打开链接
首先我们想一下如果甲乙都仅沿着两条线段向前跑,那么他们之间的最短和最长距离怎么算? 假设甲的速度向量为v1(速度向量指甲单位时间所走的位移向量),乙的速度向量为v2. 那么可以把甲看成是静止不动的,而让乙一个人以v2-v1的速度向量去运动(画图验证下).
且假设甲和乙都运动了t秒时间,那么我们可以知道上面的过程类似于甲不动,乙从自己的起点运动了t*(v2-v1)的位移向量. 而乙已知起点和位移向量,那么它就是在一条线段上. 最终我们要求的就是甲这个不动的点到乙这条线段的最大和最小距离即可.(仔细体会下)
下面我们回到原问题. 原问题是一段一段的线段连接起来的折线,但是在某一段时刻,甲乙必然都是同时在走直线段的.
我们只要把握住甲乙的当前点和下一个拐点这两个信息即可.
假设当前甲在Pa点,乙在Pb点.而他们的下一个拐点是Sa和Sb,(由于总距离已知且他们花的时间相同,所以他们的速度va和vb已知).
所以我们可以知道到底甲先到拐点还是乙先到拐点,那么他们在到达拐点的这段时间内就都是走的直线段. 然后我们处理这段的最大最小值.接着更新他们的当前位置点和下一个拐点即可接着循环处理,一直到终点.
Dog Distance
Submit Status Description
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 maximum and the minimum distance between the two dogs in their whole journey.
Mathematically, Dog Distance = {max (D(a)) 0 <= a <= T} – {min (D(b)) 0 <= b <= T} Given the paths of the two dogs, your job is to find the dog distance. Each path will be represented using N points, (P1 P2 P3 ... PN). The dog following this path will start from P1 and follow the line joining withP2, and then it will follow the line joining P2-P3, then P3-P4 and so on until it reaches Pn. InputInput starts with an integer I(I≤1000), the number of test cases. Each test case starts with 2 positive integers A(2≤A≤50),B(2≤B≤50). The next line contains the coordinates of A points with the format X1 Y1 X2 Y2 ...XA YA, (0≤Xi,Yi≤1000). These points indicate the path taken by Ranga. The next line contains B points in the same format. These points indicate the path taken by Banga. All distance units are given in meters and consecutive points are distinct. All the given coordinates are integers. Note that the values of T, R and S are unknown to us. OutputFor each case, output the case number first. Then output the dog distance rounded to the nearest integer. Look at the samples for exact format.
Source Root :: Prominent Problemsetters :: Sohel Hafiz Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 4. Geometry :: Geometric Computations in 2D :: Examples Submit Status |
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double eps=1e-8; int dcmp(double x) { if(fabs(x)<eps) return 0; return (x<0)?-1:1; } struct Point { double x,y; Point(double _x=0,double _y=0):x(_x),y(_y){} }; Point operator+(Point A,Point B){return Point(A.x+B.x,A.y+B.y);} Point operator-(Point A,Point B){return Point(A.x-B.x,A.y-B.y);} Point operator*(Point A,double p){return Point(A.x*p,A.y*p);} Point operator/(Point A,double p){return Point(A.x/p,A.y/p);} bool operator<(const Point& a,const Point& b){return a.x<b.x||(a.x==b.x&&a.y<b.y);} bool operator==(const Point& a,const Point& b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;} double Dot(Point A,Point B) {return A.x*B.x+A.y*B.y;} double Lenth(Point A) {return sqrt(Dot(A,A));} double Angle(Point A,Point B) {return acos(Dot(A,B)/Lenth(A)/Lenth(B));} double Angle(Point v) {return atan2(v.y,v.x);} double Cross(Point A,Point B){return A.x*B.y-A.y*B.x;} double DistanceToSegment(Point P,Point A,Point B) { if(A==B) return Lenth(P-A); Point v1=B-A,v2=P-A,v3=P-B; if(dcmp(Dot(v1,v2))<0) return Lenth(v2); else if(dcmp(Dot(v1,v3))>0) return Lenth(v3); else return fabs(Cross(v1,v2))/Lenth(v1); } int n,m; Point aa[55],bb[55]; double LenA,LenB; double MX,MI; void update(Point P,Point A,Point B) { MI=min(MI,DistanceToSegment(P,A,B)); MX=max(MX,max(Lenth(P-A),Lenth(P-B))); } int main() { int T_T,cas=1; scanf("%d",&T_T); while(T_T--) { LenA=0,LenB=0; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) { scanf("%lf%lf",&aa[i].x,&aa[i].y); if(i) LenA+=Lenth(aa[i]-aa[i-1]); } for(int i=0;i<m;i++) { scanf("%lf%lf",&bb[i].x,&bb[i].y); if(i) LenB+=Lenth(bb[i]-bb[i-1]); } MI=1e9,MX=-1e9; int sa=0,sb=0; Point pa=aa[0],pb=bb[0]; while(sa<n-1&&sb<m-1) { double la=Lenth(aa[sa+1]-pa); double lb=Lenth(bb[sb+1]-pb); double t=min(la/LenA,lb/LenB); Point va=(aa[sa+1]-pa)/la*t*LenA; Point vb=(bb[sb+1]-pb)/lb*t*LenB; update(pa,pb,pb+vb-va); pa=pa+va; pb=pb+vb; if(pa==aa[sa+1]) sa++; if(pb==bb[sb+1]) sb++; } printf("Case %d: %.0lf\n",cas++,MX-MI); } return 0; }