本文主要是介绍poj 2187 凸包or旋转qia壳法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
给n(50000)个点,求这些点与点之间距离最大的距离。
解析:
先求凸包然后暴力。
或者旋转卡壳大法。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long longusing namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);/
struct Point
{double x, y;Point(double x = 0, double y = 0) : x(x), y(y) {}
};bool cmp(Point A, Point B)
{if (A.x == B.x)return A.y < B.y;return A.x < B.x;
}typedef Point Vector;Vector operator + (Vector A, Vector B)
{return Vector(A.x + B.x, A.y + B.y);
}Vector operator - (Point A, Point 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);
}//用于判断相等
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;
}//求两个向量相夹的面积
double Area2(Point A, Point B, Point C)
{return Cross(B - A, C - A);
}//旋转rad弧度
Vector Rotate(Vector A, double rad)
{return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}//向量的单位法线即向量左转90°
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;
}//点P到直线AB的距离
double DistanceToLine(Point P, Point A, Point B)
{Vector v1 = B - A;Vector v2 = P - A;return fabs(Cross(v1, v2)) / Length(v1);
}//点到直线的距离
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)) < 0)return Length(v2);else if (dcmp(Dot(v1, v3)) > 0)return Length(v3);elsereturn fabs(Cross(v1, v2)) / Length(v1);
}//判断线段是否相交
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);double 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 ConvexPolygonArea(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.0;
}//° -> 弧度
double torad(double deg)
{return deg / 180 * pi;
}//求凸包 返回点个数 ch为凸包的点
int ConvexHull(Point* p, int n, Point* ch)
{sort(p, p + n);int m = 0;for (int 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 (int 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--;return m;
}Point readPoint()
{double x, y;scanf("%lf %lf", &x, &y);return Point(x, y);
}//
const int maxn = 50000 + 10;int n;
Point p[maxn << 1];
Point ch[maxn << 1];double dist(Point a, Point b)
{return Dot(a - b, a - b);
}void bruteForce()
{double ans = 0;int m = ConvexHull(p, n, ch);for (int i = 0; i < m; i++){for (int j = i + 1; j < m; j++){ans = max(ans, dist(ch[i], ch[j]));}}printf("%.0lf\n", ans);
}void rotatingCalipers()
{double ans = 0;int m = ConvexHull(p, n, ch);if (m == 2){printf("%.0lf\n", dist(ch[0], ch[1]));return;}int i = 0, j = 0;for (int k = 0; k < m; k++){if (ch[k] < ch[i])i = k;if (ch[j] < ch[k])j = k;}int si = i, sj = j;while (i != sj || j != si){ans = max(ans, dist(ch[i], ch[j]));if (Cross(ch[(i + 1) % m] - ch[i], ch[(j + 1) % m] - ch[j]) < 0){i = (i + 1) % m;}else{j = (j + 1) % m;}}printf("%.0lf\n", ans);
}int main()
{
#ifdef LOCALfreopen("in.txt", "r", stdin);
#endif // LOCALwhile (~scanf("%d", &n)){for (int i = 0; i < n; i++){p[i] = readPoint();}rotatingCalipers();}return 0;
}
这篇关于poj 2187 凸包or旋转qia壳法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!