题意:两条狗分别沿各自的线段式路线匀速运动,求俩狗相距最远最近差值。
一开始的思路是这样的,最远和最近距离应该都会在拐点处取到,所以去遍历两只狗每一个拐点,求距离,然后样例不过,发现第二个样例的最短距离并不取在拐点处,即两只狗都在某段路的中间时取到了最小值。
思路想好之后,要仔细回顾找它的反例。
正确的思路:把两个相对运动转换成1个点不运动,另一个点相对于这个点运动,经过速度分向量求和,因为是匀速运动,速度不变,所以这个相对运动是直线的,转化成二维点到直线距离。
之后就是漫长的bug,3天才断断续续A掉,最后的指针加1问题调了近一天,期间太多的不确定性,不自信,无法深入思考下去,浪费了很多的时间,也许第一次做的时候,细心一点,坚持一点,就能A掉吧,所以你需坚持一种东西在心中,去克服这样那样的想法,或者说什么都不去想,做题就好了嘛。
AC代码:
#include<bits/stdc++.h>
using namespace std;
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
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); }
bool operator < (const Point&a,const Point&b){
return a.x<b.x||(a.x==b.x && a.y<b.y);
}
const double eps=1e-10;
int dcmp(double x){
if(fabs(x)<eps) return 0; else return x < 0 ? -1 : 1;
}
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 Length(Vector A) { return sqrt(Dot(A,A)); }
double Angle(Vector A,Vector B) { return acos(Dot(A,B) / Length(A) / Length(B) ); }
double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; }
/**
first input right Edge,Cross product be Positive.
*/
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));
}
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;
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1), c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
bool OnSegment(Point p,Point a1,Point a2){
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
double distance_(Point A,Point B){
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
Point SolutionThePoint(Point A,Point B,double dis){
double D = distance_(A,B);
double Proportion = dis / D;
return A + (B-A)*Proportion;
}
double Min,Max;
double DistanceToSegment(Point P,Point A,Point B){
if(A==B) return Length(P-A);
Vector v1=B-A,v2=P-A,v3=P-B;
if(dcmp(Dot(v1,v2))<0) return Length(v2);
else if(dcmp(Dot(v1,v3))>0) return Length(v3);
else return fabs(Cross(v1,v2)) / Length(v1);
}
void update(Point P,Point A,Point B){
// cout<<"P:"<<P.x<<","<<P.y<<"__A:"<<A.x<<","<<A.y<<"__B:"<<B.x<<","<<B.y<<endl;
Min = min(Min,DistanceToSegment(P,A,B));
Max = max(Max,DistanceToSegment(P,A,B));
// Max = max(Max,Length(P-B));
// cout<<"Min"<<Min<<"___"<<"Max"<<Max<<endl;
}
int main()
{
int t,c=1;
scanf("%d",&t);
int a,b;
while(t--){
scanf("%d%d",&a,&b);
Point pa[60],pb[60];
double Da[60],Db[60];
double Lena=0.0,Lenb=0.0;
for(int i=0;i<a;i++) { scanf("%lf%lf",&pa[i].x,&pa[i].y); if(i!=0) Lena+=(Da[i]=Length(pa[i]-pa[i-1])); }
for(int i=0;i<b;i++) { scanf("%lf%lf",&pb[i].x,&pb[i].y); if(i!=0) Lenb+=(Db[i]=Length(pb[i]-pb[i-1])); }
int _a=1,_b=1;
Point sa=pa[0],sb=pb[0];
double va=Lena,vb=Lenb;
// cout<<va<<vb<<endl;
Min=0x7f7f7f7f;
Max=-0x7f7f7f7f;
while(_a<a&&_b<b){
double T=min(Length(pa[_a]-sa)/va,Length(pb[_b]-sb)/vb);
Vector v1 = (pa[_a]-sa)/Length(pa[_a]-sa)*T*va;
Vector v2 = (pb[_b]-sb)/Length(pb[_b]-sb)*T*vb;
update(sa,sb,sb+v2-v1);
sa=sa+v1;
sb=sb+v2;
// cout<<"sa"<<sa.x<<","<<sa.y<<"__sb"<<sb.x<<","<<sb.y<<endl;
if(sa==pa[_a]) _a++;
if(sb==pb[_b]) _b++;
}
printf("Case %d: %.0lf\n",c++,Max-Min);
// cout<<Max-Min<<endl;
}
return 0;
}
附上三鲜之所在博客一段话:
他属于很清楚自己在做什么的人,因此没有动不动就看discuss、解题报告的陋习。尽管我有时候觉得他比较懒,并没有花太多的精力在ACM之中,但由于他的清醒和踏实,只要做一点,就能进步一点,只要花心思,就能见效果。
血洗几何