首先我对着第一篇题解写了发凸包。
#include <bits/stdc++.h>
using namespace std;
#define int long long
inline int read()
{
int sum = 0, nega = 1;
char ch = getchar();
while (ch > '9'||ch < '0')
{
if (ch == '-') nega = -1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') sum = sum * 10 + ch - '0', ch = getchar();
return sum * nega;
}
const int N = 1e5 + 9;
const double eps = 1e-10;
int n;
struct point
{
double x, y;
}p[N], sta[N];
int top = 0;
inline double check(point a1, point a2, point b1, point b2)
{
return (a2.x - a1.x) * (b2.y - b1.y) - (b2.x - b1.x) * (a2.y - a1.y);
}
inline double dis(point a1, point a2)
{
return sqrt((a1.x - a2.x) * (a1.x - a2.x) + (a1.y - a2.y) * (a1.y - a2.y));
}
inline bool cmp(point x, point y)
{
double qwq = check(p[1], x, p[1], y);
if(qwq > eps) return 1;
if(qwq > -eps && dis(p[1], x) < dis(p[1], y)) return 1;
return 0;
}
signed main()
{
n = read();
for (int i = 1; i <= n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
for (int i = 2; i <= n; i++)
if(p[1].y - p[i].y > eps) swap(p[1], p[i]);
sort(p + 2, p + n + 1, cmp);
sta[++top] = p[1];
for (int i = 2; i <= n; i++)
{
while(top > 1 && check(sta[top - 1], sta[top], sta[top], p[i]) < -eps) top--;
sta[++top] = p[i];
}
sta[++top] = p[1];
double ans = 0;
for (int i = 1; i < top; i++) ans = ans + dis(sta[i], sta[i + 1]);
printf("%.2lf\n", ans);
return 0;
}
结果挂成了 91。
然后把
if(qwq > -eps && dis(p[1], x) < dis(p[1], y)) return 1;
改成了
if(qwq > -eps && dis(p[0], x) < dis(p[0], y)) return 1;
就过了。
但是感觉排序的时候应该按照到第一个点的距离来排吧
求大佬解答是为什么