求凸包
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
struct node{
double x, y;
friend bool operator < (node a, node b) {
if (fabs(a.x - b.x) > eps)
return a.x - b.x < eps;
return a.y - b.y < eps;
}
}p[100005], stk[100005];
int n, top;
double ans;
double dis(node a, node b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double cross(node a, node b) {
return a.x * b.y - a.y * b.x;
}
node mis(node a, node b) {
return node{a.x - b.x, a.y - b.y};
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
sort(p + 1, p + 1 + n);
top = 0;
for (int i = 1; i <= n; i++) {
while (top >= 2 && cross(mis(stk[top], stk[top - 1]), mis(p[i], stk[top])) <= eps) top--;
stk[++top] = p[i];
}
for (int i = 1; i < top; i++) ans += dis(stk[i], stk[i + 1]);
top = 0;
for (int i = n; i >= 1; i--) {
while (top >= 2 && cross(mis(stk[top], stk[top - 1]), mis(p[i], stk[top])) <= eps) top--;
stk[++top] = p[i];
}
for (int i = 1; i < top; i++) ans += dis(stk[i], stk[i + 1]);
printf("%.2lf", ans);
return 0;
}
旋转卡壳
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
struct node{
double x, y;
friend bool operator < (node a, node b) {
if (fabs(a.x - b.x) > eps)
return a.x - b.x < eps;
return a.y - b.y < eps;
}
}p[100005], stk[100005];
int n, top, mid;
double ans;
double dis(node a, node b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double cross(node a, node b) {
return a.x * b.y - a.y * b.x;
}
node mis(node a, node b) {
return node{a.x - b.x, a.y - b.y};
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
sort(p + 1, p + 1 + n);
for (int i = 1; i <= n; i++) {
while (top >= 2 && cross(mis(stk[top], stk[top - 1]), mis(p[i], stk[top])) <= eps) top--;
stk[++top] = p[i];
}
mid = top;
for (int i = n; i >= 1; i--) {
while (top >= mid + 1 && cross(mis(stk[top], stk[top - 1]), mis(p[i], stk[top])) <= eps) top--;
stk[++top] = p[i];
}
top--;
int now = 2;
for (int i = 1; i <= top; i++) {
while (cross(mis(stk[i + 1], stk[i]), mis(stk[now], stk[i])) < cross(mis(stk[i + 1], stk[i]), mis(stk[now + 1], stk[i]))) {
now = now % top + 1;
}
ans = max(ans, (stk[i].x - stk[now].x) * (stk[i].x - stk[now].x) + (stk[i].y - stk[now].y) * (stk[i].y - stk[now].y));
}
printf("%.0lf", ans);
return 0;
}