凸包+选择卡壳 ..... 测试模板
Beauty Contest
Description
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.
Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms. Input
* Line 1: A single integer, N
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm Output
* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.
Sample Input 4 0 0 0 1 1 1 1 0 Sample Output 2 Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
Source
USACO 2003 Fall
|
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const double eps=1e-8; const double pi=acos(-1.0); const double inf=1e20; const int maxp=51111; int dblcmp(double d) { if (fabs(d)<eps)return 0; return d>eps?1:-1; } inline double sqr(double x){return x*x;} struct point { double x,y; point(){} point(double _x,double _y): x(_x),y(_y){}; void input() { scanf("%lf%lf",&x,&y); } void output() { printf("%.2f %.2f\n",x,y); } bool operator==(point a)const { return dblcmp(a.x-x)==0&&dblcmp(a.y-y)==0; } bool operator<(point a)const { return dblcmp(a.x-x)==0?dblcmp(y-a.y)<0:x<a.x; } double len() { return hypot(x,y); } double len2() { return x*x+y*y; } double distance(point p) { return hypot(x-p.x,y-p.y); } point add(point p) { return point(x+p.x,y+p.y); } point sub(point p) { return point(x-p.x,y-p.y); } point mul(double b) { return point(x*b,y*b); } point div(double b) { return point(x/b,y/b); } double dot(point p) { return x*p.x+y*p.y; } double det(point p) { return x*p.y-y*p.x; } double rad(point a,point b) { point p=*this; return fabs(atan2(fabs(a.sub(p).det(b.sub(p))),a.sub(p).dot(b.sub(p)))); } point trunc(double r) { double l=len(); if (!dblcmp(l))return *this; r/=l; return point(x*r,y*r); } point rotleft() { return point(-y,x); } point rotright() { return point(y,-x); } point rotate(point p,double angle)//绕点p逆时针旋转angle角度 { point v=this->sub(p); double c=cos(angle),s=sin(angle); return point(p.x+v.x*c-v.y*s,p.y+v.x*s+v.y*c); } //单位向量 point horunit(){return this->div(this->len());} //左转的单位法向量 point verunit(){return point(-y,x).div(this->len());} //向向量v的方向移动距离d point todir(point v,double d) { return point(x+v.x*d,y+v.y*d); } ///象限的极角排序 }; struct line { point a,b; line(){} line(point _a,point _b) { a=_a; b=_b; } bool operator==(line v) { return (a==v.a)&&(b==v.b); } //倾斜角angle line(point p,double angle) { a=p; if (dblcmp(angle-pi/2)==0) { b=a.add(point(0,1)); } else { b=a.add(point(1,tan(angle))); } } //ax+by+c=0 line(double _a,double _b,double _c) { if (dblcmp(_a)==0) { a=point(0,-_c/_b); b=point(1,-_c/_b); } else if (dblcmp(_b)==0) { a=point(-_c/_a,0); b=point(-_c/_a,1); } else { a=point(0,-_c/_b); b=point(1,(-_c-_a)/_b); } } void input() { a.input(); b.input(); } void output() { printf("(%.2lf,%.2lf) --- (%.2lf,%.2lf)\n",a.x,a.y,b.x,b.y); } void adjust() { if (b<a)swap(a,b); } double length() { return a.distance(b); } /// a-->b向左垂直方向平移距离d line transhor(double d) { point v=b.sub(a); point dir=v.verunit(); a=a.todir(dir,d); b=b.todir(dir,d); return *this; } double angle()//直线倾斜角 0<=angle<180 { double k=atan2(b.y-a.y,b.x-a.x); if (dblcmp(k)<0)k+=pi; if (dblcmp(k-pi)==0)k-=pi; return k; } //点和线段关系 //1 在逆时针 //2 在顺时针 //3 平行 int relation(point p) { int c=dblcmp(p.sub(a).det(b.sub(a))); if (c<0)return 1; if (c>0)return 2; return 3; } bool pointonseg(point p) { return dblcmp(p.sub(a).det(b.sub(a)))==0&&dblcmp(p.sub(a).dot(p.sub(b)))<=0; } bool parallel(line v) { return dblcmp(b.sub(a).det(v.b.sub(v.a)))==0; } //2 规范相交 //1 非规范相交 //0 不相交 int segcrossseg(line v) { int d1=dblcmp(b.sub(a).det(v.a.sub(a))); int d2=dblcmp(b.sub(a).det(v.b.sub(a))); int d3=dblcmp(v.b.sub(v.a).det(a.sub(v.a))); int d4=dblcmp(v.b.sub(v.a).det(b.sub(v.a))); if ((d1^d2)==-2&&(d3^d4)==-2)return 2; return (d1==0&&dblcmp(v.a.sub(a).dot(v.a.sub(b)))<=0|| d2==0&&dblcmp(v.b.sub(a).dot(v.b.sub(b)))<=0|| d3==0&&dblcmp(a.sub(v.a).dot(a.sub(v.b)))<=0|| d4==0&&dblcmp(b.sub(v.a).dot(b.sub(v.b)))<=0); } int linecrossseg(line v)//*this seg v line { int d1=dblcmp(b.sub(a).det(v.a.sub(a))); int d2=dblcmp(b.sub(a).det(v.b.sub(a))); if ((d1^d2)==-2)return 2; return (d1==0||d2==0); } //0 平行 //1 重合 //2 相交 int linecrossline(line v) { if ((*this).parallel(v)) { return v.relation(a)==3; } return 2; } point crosspoint(line v) { double a1=v.b.sub(v.a).det(a.sub(v.a)); double a2=v.b.sub(v.a).det(b.sub(v.a)); return point((a.x*a2-b.x*a1)/(a2-a1),(a.y*a2-b.y*a1)/(a2-a1)); } double dispointtoline(point p) { return fabs(p.sub(a).det(b.sub(a)))/length(); } double dispointtoseg(point p) { if (dblcmp(p.sub(b).dot(a.sub(b)))<0||dblcmp(p.sub(a).dot(b.sub(a)))<0) { return min(p.distance(a),p.distance(b)); } return dispointtoline(p); } point lineprog(point p) { return a.add(b.sub(a).mul(b.sub(a).dot(p.sub(a))/b.sub(a).len2())); } point symmetrypoint(point p) { point q=lineprog(p); return point(2*q.x-p.x,2*q.y-p.y); } }; struct polygon { int n; point p[maxp]; line l[maxp]; void input() { scanf("%d",&n); for (int i=0;i<n;i++) { p[i].input(); } } void add(point q) { p[n++]=q; } void getline() { for (int i=0;i<n;i++) { l[i]=line(p[i],p[(i+1)%n]); } } struct cmp { point p; cmp(const point &p0){p=p0;} bool operator()(const point &aa,const point &bb) { point a=aa,b=bb; int d=dblcmp(a.sub(p).det(b.sub(p))); if (d==0) { return dblcmp(a.distance(p)-b.distance(p))<0; } return d>0; } }; void norm() { point mi=p[0]; for (int i=1;i<n;i++)mi=min(mi,p[i]); sort(p,p+n,cmp(mi)); } void getconvex(polygon &convex) ///没有在凸包边上的点 '<='-->'<' { int i,j,k; sort(p,p+n); convex.n=n; for (i=0;i<min(n,2);i++) { convex.p[i]=p[i]; } if (n<=2)return; int &top=convex.n; top=1; for (i=2;i<n;i++) { while (top&&convex.p[top].sub(p[i]).det(convex.p[top-1].sub(p[i]))<=0) top--; convex.p[++top]=p[i]; } int temp=top; convex.p[++top]=p[n-2]; for (i=n-3;i>=0;i--) { while (top!=temp&&convex.p[top].sub(p[i]).det(convex.p[top-1].sub(p[i]))<=0) top--; convex.p[++top]=p[i]; } } double rocalipers() ///卡壳 要求不能有共线的点 { getline(); int e=1; double ret=0; for(int i=0;i<n;i++) ///枚举边进行旋转卡壳 { while(fabs((p[i].sub(p[e+1])).det(p[(i+1)%n].sub(p[e+1]))) >fabs((p[i].sub(p[e])).det(p[(i+1)%n].sub(p[e])))) e=(e+1)%n; ret=max(ret,max((p[i].sub(p[e])).len2(),(p[(i+1)%n].sub(p[e])).len2())); } return ret; } }; int n; int main() { while(scanf("%d",&n)!=EOF) { polygon pg,cov; pg.n=0; for(int i=0;i<n;i++) { double x,y; scanf("%lf%lf",&x,&y); pg.add(point(x,y)); } pg.getconvex(cov); double len2=cov.rocalipers(); printf("%.0lf\n",len2); } return 0; }