标签:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 5e4+10; typedef struct Point{ int x,y; bool operator <(const struct Point &B)const{ if(x==B.x) return y<B.y; return x<B.x; } bool operator ==(const struct Point &B)const{ return (x-B.x)==0&&(y-B.y)==0; } }Vector; Vector p[maxn],st[maxn]; int Cross(Point a,Point b,Point o) { return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y); } int ConvexHull(Point *p,int n,Point *st){ sort(p,p+n); n = unique(p,p+n)-p; int m=0; for(int i=0;i<n;i++){ while(m>1&&Cross(st[m-1],p[i],st[m-2])<=0) m--; st[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--){1 while(m>k&&Cross(st[m-1],p[i],st[m-2])<=0) m--; st[m++]=p[i]; } if(n>1) m--; return m; } int dis(Point a,Point b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } //旋转卡壳求凸包的宽度 double rotating_calipers(Point *p,int n){ int k = 1; double ans = 0x7FFFFFFF; p[n] = p[0]; for(int i=0;i<n;i++){ while(fabs(cross(p[i],p[i+1],p[k])) < fabs(cross(p[i],p[i+1],p[k+1]))) k = (k+1) % n; double tmp = fabs(cross(p[i],p[i+1],p[k])); double d = dist(p[i],p[i+1]); ans = min(ans,tmp/d); } return ans; } int main() { int n; while(~scanf("%d",&n)){ for(int i=0;i<n;i++){ scanf("%d%d",&p[i].x,&p[i].y); } int m = ConvexHull(p,n,st); int ans = rotatint_calipers(st,m); printf("%d\n",ans); } return 0; }
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const int maxn = 5e4+10; const double eps = 1e-10; int dcmp(double x){ if(fabs(x)<=0) return 0; if(x>0) return 1; else return -1; } typedef struct Point{ double x,y; bool operator <(const struct Point &B)const{ if(x==B.x) return y<B.y; return x<B.x; } bool operator ==(const struct Point &B)const{ return dcmp(x-B.x)==0&&dcmp(y-B.y)==0; } }Vector; Vector p[maxn],st[maxn]; double Cross(Point a,Point b,Point o) { return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y); } int ConvexHull(Point *p,int n,Point *st){ sort(p,p+n); n = unique(p,p+n)-p; int m=0; for(int i=0;i<n;i++){ while(m>1&&Cross(st[m-1],p[i],st[m-2])<=0) m--; st[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--){ while(m>k&&Cross(st[m-1],p[i],st[m-2])<=0) m--; st[m++]=p[i]; } if(n>1) m--; return m; } double dis(Point a,Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double rotating_calipers(Point *p,int n){ double ans = 0; int j,k; for(int i=0;i<n;i++){ j=(i+1)%n; k=(j+1)%n; while(fabs(Cross(p[i+1],p[k],p[i]))<fabs(Cross(p[i+1],p[k+1],p[i]))) k=(k+1)%n; while(j!=i&&k!=i){ ans=max(ans,fabs(Cross(p[i],p[k],p[j]))); while(fabs(Cross(p[i],p[k],p[j]))<fabs(Cross(p[i],p[(k+1)%n],p[j]))) k=(k+1)%n; j=(j+1)%n; } } return ans/2.0; } int main() { int n; while(~scanf("%d",&n)){ if(n==-1) break; for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); int m = ConvexHull(p,n,st); //cout<<"m "<<m<<endl; if(m<3) puts("0.00"); if(m==3) printf("%.2lf\n",fabs(Cross(st[0],st[1],st[2]))/2.0); else{ double ans = rotating_calipers(st,m); printf("%.2lf\n",ans); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/bigbigship/article/details/47664363