[hoj 1489]ATDS[凸包][旋转卡壳]

题意:

求一堆点中两点之间的最大距离.

思路:

凸包+旋转卡壳. [大水]


注意 n==1 的情况.

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;
const int MAXN = 6005;
struct point
{
    double x,y;
}res[MAXN],p[MAXN];
int top,n;
/*cross product*/
double cross(point a, point b, point o)//OA X OB
{
    return (a.x - o.x)*(b.y - o.y) - (b.x - o.x)*(a.y - o.y);
}
/*square sum*/
double sqsm(point a, 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)
{
    if(a.x - b.x > EPS || b.x - a.x > EPS)  return a.x < b.x;
    return a.y < b.y;
}
void Graham()
{
    sort(p, p+n, cmp);
    res[0] = p[0];
    res[1] = p[1];
    top = 1;
    for(int i=2;i<n;i++)
    {
        while(top && cross(p[i], res[top-1], res[top])<=EPS)
            top--;
        res[++top] = p[i];
    }
    int mark = top;
    res[++top] = p[n-2];
    for(int i=n-3;i>=0;i--)
    {
        while(top>mark && cross(p[i], res[top-1], res[top])<=EPS)
            top--;
        res[++top] = p[i];
    }
}

int main()
{
    while(scanf("%d",&n)==1)
    {
        for(int i=0;i<n;i++)
            scanf("%lf %lf",&p[i].x,&p[i].y);
        if(n==1)
        {
            printf("0.00\n");
            continue;
        }
        Graham();
        double dia = 0;
        int q = 1;
        for(int i=0;i<top;i++)
        {
            while(fabs(cross(res[i],res[i+1],res[q]))<fabs(cross(res[i],res[i+1],res[q+1])))
                q = (q+1)%top;
            dia = max(dia, max(sqsm(res[i],res[q]), sqsm(res[i+1], res[q+1])));
        }
        dia = sqrt(dia);
        printf("%.2lf\n",dia);
    }
}


你可能感兴趣的:([hoj 1489]ATDS[凸包][旋转卡壳])