zoukankan      html  css  js  c++  java
  • LA 4728 旋转卡壳算法求凸包的最大直径


    #include<iostream> #include<cstdio> #include<cmath> #include<vector> #include<algorithm> using namespace std; struct Point { int x, y; Point(int x=0, int y=0):x(x),y(y) { } }; typedef Point Vector; Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); } int Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; } int Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; } int Dist2(const Point& A, const Point& B) { return (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y); } bool operator < (const Point& p1, const Point& p2) { return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y); } bool operator == (const Point& p1, const Point& p2) { return p1.x == p2.x && p1.y == p2.y; } int max(int a,int b) { return a>b?a:b; } vector<Point> ConvexHull(vector<Point>& p) //求凸包
    { sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end()), p.end()); int i,n = p.size(); int m = 0; vector<Point> ch(n+1); for(i = 0; i < n; i++) { while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for(i = n-2; i >= 0; i--) { while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--; ch[m++] = p[i]; } if(n > 1) m--; ch.resize(m); return ch; } int diameter2(vector<Point>& points)//求凸包的最大直径(旋转卡壳算法)
    { vector<Point> p = ConvexHull(points); int n = p.size(); if(n == 1) return 0; if(n == 2) return Dist2(p[0], p[1]); p.push_back(p[0]); int ans = 0; int i=0,j=1; for(;i<n;i++) { while(Cross(p[i+1]-p[i], p[j+1]-p[i]) > Cross(p[i+1]-p[i], p[j]-p[i])) j=(j+1)%n; ans=max(ans,max(Dist2(p[i],p[j]),Dist2(p[i+1],p[j+1]))); } return ans; } int main() { int T,i,n,x,y,w; vector<Point> P; scanf("%d",&T); while(T--) { P.clear(); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d%d", &x, &y, &w); P.push_back(Point(x, y)); P.push_back(Point(x+w, y)); P.push_back(Point(x, y+w)); P.push_back(Point(x+w, y+w)); } printf("%d ", diameter2(P)); } return 0; }
  • 相关阅读:
    iOS开发中应用程序图标和启动图片的尺寸
    Localization native development region 设置属性(转)
    iOS 开发之 ZBarSDK 二维码扫描自定义二维码扫描页面(二)
    iOS 开发之 ZBarSDK 二维码扫描 静态库不支持64bit完美解决(一)
    iOS 播放Gif动画
    使用GCD异步下载图片 可以中途停止下载
    GCD线程队列 MJ(转)
    iOS 开发之头部滚动展示视图
    iOS 版本更新 (转)
    iOS App打包发布 (转)
  • 原文地址:https://www.cnblogs.com/xiong-/p/3409215.html
Copyright © 2011-2022 走看看