题目地址:点这里
一个有一点物理情景的题目. 如果A,B 都在一条线段上运动,其实固定A,B相对于A沿着一条线段在运动,这个子问题里面,就可以更新最大最小值。
用一个Sa,Sb 变量维护当前刚刚经过的结点编号。 一直维护到有一个刚好到了最后一个结点(额,应该是同时到达的)。 然后求出总路程长度以后就可以假设速度就是这个。(时间一样)。
最后的效果就是,Pa 在折线A上运动,遍历的地方,要么是P[i] ,要么是Pb==Q[j] 的时候。
更新的时候,其实是--我们知道它们位置关系的起点和终点。 而且知道轨迹是线段,那么整个过程就清晰了。
注意是保存到最近的整数。
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
const double eps=1e-10;
using namespace std;
struct Point{
double x;
double y;
Point(double x=0,double y=0):x(x),y(y){}
void operator<<(Point &A) {cout<<A.x<<' '<<A.y<<endl;}
};
int dcmp(double x) {return (x>eps)-(x<-eps); }
typedef Point Vector;
Vector operator +(Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y);}
Vector operator -(Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }
Vector operator *(Vector A,double p) { return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p);}
// ps cout
ostream &operator<<(ostream & out,Point & P) { out<<P.x<<' '<<P.y<<endl; return out;}
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(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
double Cross(Vector A,Vector B) {return A.x*B.y-B.x*A.y; }
double Length(Vector A) { return sqrt(Dot(A, A));}
double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}
double Area2(Point A,Point B,Point C ) {return Cross(B-A, C-A);}
Vector Rotate(Vector A,double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}
Vector Normal(Vector A) {double L=Length(A);return Vector(-A.y/L,A.x/L);}
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w, u)/Cross(v,w);
return P+v*t;
}
double DistanceToLine(Point P,Point A,Point B)
{
Vector v1=P-A; Vector v2=B-A;
return fabs(Cross(v1,v2))/Length(v2);
}
double DistanceToSegment(Point P,Point A,Point B)
{
if(A==B) return Length(P-A);
Vector v1=B-A;
Vector v2=P-A;
Vector v3=P-B;
if(dcmp(Dot(v1,v2))==-1) return Length(v2);
else if(Dot(v1,v3)>0) return Length(v3);
else return DistanceToLine(P, A, B);
}
Point GetLineProjection(Point P,Point A,Point B)
{
Vector v=B-A;
Vector v1=P-A;
double t=Dot(v,v1)/Dot(v,v);
return A+v*t;
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
double c1=Cross(b1-a1, a2-a1);
double c2=Cross(b2-a1, a2-a1);
double c3=Cross(a1-b1, b2-b1);
double c4=Cross(a2-b1, b2-b1);
//cout<<c1<<c2<<c3<<c4<<endl;
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0 ;
}
bool OnSegment(Point P,Point A,Point B)
{
return dcmp(Cross(P-A, P-B))==0&&dcmp(Dot(P-A,P-B))<0;
}
double PolygonArea(Point *p,int n)
{
double area=0;
for(int i=1;i<n-1;i++)
{
area+=Cross(p[i]-p[0], p[i+1]-p[0]);
}
return area/2;
}
Point read_point()
{
Point P;
scanf("%lf%lf",&P.x,&P.y);
return P;
}
double Max,Min;
const int maxn=60;
Point P[maxn],Q[maxn];
void update(Point P,Point A,Point B)
{
Min=min(Min,DistanceToSegment(P, A,B));
Max=max(Max,Length(P-A));
Max=max(Max,Length(P-B));
}
int main()
{
int T;
int A,B;
cin>>T;
for(int l=0;l<T;l++)
{
cin>>A>>B;
for(int i=0;i<A;i++)
P[i]=read_point();
for(int i=0;i<B;i++)
Q[i]=read_point();
double LenA=0,LenB=0;
for(int i=0;i<A-1;i++)
LenA+=Length(P[i+1]-P[i]);
for(int i=0;i<B-1;i++)
LenB+=Length(Q[i+1]-Q[i]);
int Sa=0,Sb=0;
Min=1e9;
Max=-1;
Point Pa=P[0];
Point Pb=Q[0];
while(Sa<A-1)
{
double La=Length(P[Sa+1]-Pa);
double Lb=Length(Q[Sb+1]-Pb);
double T=min(La/LenA,Lb/LenB);
Vector Va=(P[Sa+1]-Pa)/La*T*LenA;
Vector Vb=(Q[Sb+1]-Pb)/Lb*T*LenB;
update(Pa,Pb,Pb+Vb-Va);
Pa=Pa+Va;
Pb=Pb+Vb;
if(Pa==P[Sa+1]) Sa++;
if(Pb==Q[Sb+1]) Sb++;
}
int ans=Max-Min;
if(Max-Min-ans>=0.5) ans++;
printf("Case %d: %d\n",l+1,ans);
}
}