思路:枚举所有点,计算两点长度,同时在搜索所有点,判断是否有共线的,有的话,只算距离最长的就可以了。对于多个点在同一直线上,最长距离肯定是两端的点,所以需要预排序。时间复杂度O(n*n*n).
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define eps 1e-8
const int MAX = 309;
const int INF = 1<<30;
struct Point {
double x, y;
};
Point p[MAX];
bool flag[MAX][MAX];
double direction(int i, int j, int k)
{
return ((p[j].x-p[i].x)*(p[k].y-p[i].y)-(p[j].y-p[i].y)*(p[k].x-p[i].x));
}
double getdis(int i, int j)
{
double dx,dy;
dx = p[j].x - p[i].x;
dy = p[j].y - p[i].y;
return (sqrt(dx*dx + dy*dy));
}
double direction(Point a, Point b)
{
return ((a.x-p[0].x)*(b.y-p[0].y) - (a.y-p[0].y)*(b.x-p[0].x));
}
double getdis(Point a)
{
double dx,dy;
dx = a.x - p[0].x;
dy = a.y - p[0].y;
return (sqrt(dx*dx + dy*dy));
}
int cmp(Point a, Point b)
{
if(fabs(direction(a, b)) < eps)
return getdis(a) < getdis(b);
else if(direction(a, b) < 0)
return 1;
else
return 0;
}
int main()
{
int i, j, k, n;
double ans = 0, ma;
scanf("%d", &n);
k = 0;
for (i = 0; i < n; ++i) {
scanf("%lf %lf", &p[i].x, &p[i].y);
if ((p[i].x == p[k].x && p[i].y < p[k].y) || (p[i].x < p[k].x))
k = i;
}
swap(p[0], p[k]);
sort(p+1, p+n, cmp);
memset(flag, 0, sizeof(flag));
for (i = 0; i < n; ++i)
for (j = i+1; j < n; ++j) {
ma = getdis(i, j);
for (k = j + 1; k < n; ++k) {
if(fabs(direction(i, j, k)) < eps){
ma = max(ma, getdis(i, k));
ma = max(ma, getdis(j, k));
flag[i][k] = flag[k][i] = 1;
flag[j][k] = flag[k][j] = 1;
}
}
if (!flag[i][j]) {
ans += ma;
flag[i][j] = 1;
flag[j][i] = 1;
}
}
printf("%.0lf\n", ans);
return 0;
}