求凸包的Graham算法:先极角排序,然后O(n)复杂度解决,具体做法见代码。
POJ:1113 Wall
题解:求凸包,且要求城墙也城堡之间有L的距离,只需要加上L为直径的圆周长即可。
#include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <cstring> using namespace std; const int MAXN = 1003; const double pi = acos(-1.0); struct POINT { int x, y; } p[MAXN], hull[MAXN]; int n, l, m; inline int cross(POINT &p1, POINT &p2, POINT &p0) { return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y); } bool cmp(POINT a, POINT b) { int t = cross(a, b, p[0]); if (t > 0) return true; if (t < 0) return false; if (abs(a.x-p[0].x)+abs(a.y-p[0].y) < abs(b.x-p[0].x)+abs(b.y-p[0].y)) return true; else return false; } void Graham() { m = 0; hull[m++] = p[0]; hull[m++] = p[1]; for (int i=2; i<n; i++) { while (cross(hull[m-2], p[i], hull[m-1]) >= 0 && m>=2) m--; hull[m++] = p[i]; } } inline double dist(POINT a, POINT b) { return sqrt((double)((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y))); } int main() { while (scanf("%d%d", &n, &l) == 2) { int x, y, k = 0; for (int i=0; i<n; i++) { scanf("%d %d", &p[i].x, &p[i].y); if ((p[k].x>p[i].x) || (p[k].x==p[i].x && p[k].y>p[i].y)) k = i; } POINT tmp = p[0]; p[0] = p[k]; p[k] = tmp; sort(p+1, p+n, cmp); Graham(); double ans = dist(hull[m-1], hull[0]); for (int i=0; i<m-1; i++) ans += dist(hull[i], hull[i+1]); ans += 2*pi*l; printf("%.0lf\n", ans); } return 0; }
POJ:2187 Beauty Contest
求N个点的最远点对。
最短点对有分治算法,复杂度为O(nlogn)。
最远点对暴力的话为O(n^2)。
这道题目先求凸包,然后利用旋转卡壳算法,可在O(nlogn)复杂度内完成。
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <cmath> using namespace std; #define N 50005 struct POINT { int x, y; } p[N], hull[N]; int n, m; inline int cross(const POINT &p1, const POINT &p2, const POINT &p0) { return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y); } inline int dist(const POINT &a, const POINT &b) { return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); } bool cmp(POINT a, POINT b) { int t = cross(a, b, p[0]); if (t > 0) return true; if (t < 0) return false; if (abs(a.x-p[0].x)+abs(a.y-p[0].y) < abs(b.x-p[0].x)+abs(b.y-p[0].y)) return true; else return false; } void Graham() { //求凸包的Graham算法 int k = 0; for (int i=1; i<n; i++) if (p[i].y<p[k].y || (p[i].y==p[k].y && p[i].x<p[k].x)) k = i; POINT tmp = p[0]; p[0] = p[k]; p[k] = tmp; sort(p+1, p+n, cmp); hull[0] = p[0], hull[1] = p[1]; m = 2; for (int i=2; i<n; i++) { while (m>=2 && cross(hull[m-2], p[i], hull[m-1])>=0) m--; hull[m++] = p[i]; } } void solve() { //旋转卡壳 int k = 1, ans = 0; hull[m] = hull[0]; for (int i=0; i<m; i++) { while (cross(hull[i+1], hull[k+1], hull[i]) > cross(hull[i+1], hull[k], hull[i])) k = (k+1)%m; ans = max(ans, max(dist(hull[i], hull[k]), dist(hull[i+1], hull[k]))); } printf("%d\n", ans); } int main() { scanf("%d", &n); for (int i=0; i<n; i++) scanf("%d%d", &p[i].x, &p[i].y); Graham(); solve(); return 0; }
POJ:Triangle
求最大的三角形的面积。易知最大的三角形的三个顶点在凸包的顶点上。
先求出凸包。记Area(i, j, k)为三角形的面积,枚举每个顶点i,初始令j=i+1, k = j+1,然后旋转边(i,j)
如果Area(i,j,k) > Area(i,j,k+1) 更新最大面积,同时旋转j,k两个点,如果Area(i,j,k) < Area(i, j, k+1),k = k + 1
旋转的复杂度为O(N),枚举 i 的复杂度为O(N),总的复杂度为O(n^2)。
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> using namespace std; #define N 50005 struct POINT { int x, y; } p[N], hull[N]; int n, m; inline int cross(const POINT &p1, const POINT &p2, const POINT &p0) { return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y); } bool cmp(POINT a, POINT b) { int t = cross(a, b, p[0]); if (t > 0) return true; if (t < 0) return false; if (abs(a.x-p[0].x)+abs(a.y-p[0].y) < abs(b.x-p[0].x)+abs(b.y-p[0].y)) return true; else return false; } void Graham() { int k = 0; for (int i=1; i<n; i++) if (p[i].x<p[k].x || (p[i].x==p[k].x && p[i].y<p[i].y)) k = i; POINT tmp = p[0]; p[0] = p[k]; p[k] = tmp; sort(p+1, p+n, cmp); hull[0] = p[0], hull[1] = p[1]; m = 2; for (int i=2; i<n; i++) { while (m>=2 && cross(hull[m-2], p[i], hull[m-1]) >= 0) m--; hull[m++] = p[i]; } } void solve() { int ans = 0; int k, j, t; for (int i=0; i<m; i++) { j = (i + 1)%m; k = (j + 1)%m; while (j != i) { while (k!=i && cross(hull[j], hull[(k+1)%m], hull[i]) > cross(hull[j], hull[k], hull[i])) k = (k + 1) % m; t = cross(hull[j], hull[k], hull[i]); if (t > ans) ans = t; j = (j + 1) % m; } } printf("%.2lf\n", fabs((double)ans/2.0)); } int main() { while (scanf("%d", &n) == 1 && n!=-1) { for (int i=0; i<n; i++) scanf("%d %d", &p[i].x, &p[i].y); Graham(); solve(); } return 0; }