【OI康复训练】POJ2187旋转卡壳

#include<cstdio>
#include<vector>
#include<cmath>
#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; }
vector<Point> ConvexHull(vector<Point>& p) {
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());

int n = p.size();
int m = 0;
vector<Point> ch(n+1);
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--;
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;
for(int u = 0, v = 1; u < n; u++) {
for(;;) {
int diff = Cross(p[u+1]-p[u], p[v+1]-p[v]);
if(diff <= 0) {
ans = max(ans, Dist2(p[u], p[v]));
if(diff == 0) ans = max(ans, Dist2(p[u], p[v+1]));
break;
}
v = (v + 1) % n;
}
}
return ans;
}

int main() {
int T=1;
while(T--) {
int n;
scanf("%d", &n);
vector<Point> points;
for(int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
points.push_back(Point(x, y));
}
printf("%d\n", diameter2(points));
}
return 0;
}

poj2187 最远点对（旋转卡壳）

2017-03-29 02:15:08

旋转卡壳 模板 【poj2187】Beauty Contest

2017-02-25 21:42:27

POJ 2187 Beauty Contest（旋转卡壳模板题）

2016-05-19 20:13:50

POJ2187【旋转卡壳】

2015-04-01 01:09:58

【POJ2187】Beauty Contest【旋转卡壳】

2016-05-26 15:16:22

POJ2187 Beauty Contest（旋转卡壳）

2012-09-21 12:27:48

计算几何——旋转卡壳【POJ2187】

2016-09-18 11:46:05

【旋转卡壳】Beauty Contest POJ2187

2013-04-14 12:55:30

poj2187 Beauty Contest（旋转卡壳）

2015-06-15 15:16:50

POJ2187 Andrew凸包+旋转卡壳

2016-01-09 16:44:17