旋转卡壳求长度(凸包直径)

版权声明:本文为博主原创文章,转载请标明原博客。 https://blog.csdn.net/sdau_fangshifeng/article/details/89764805

思路链接:

https://blog.csdn.net/ACMaker/article/details/3177045

题目链接:

http://poj.org/problem?id=2187

This is the codes: 

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
using namespace std;
const int maxn=100005;
struct Point
{
    LL x,y;
    Point() {}
    Point(LL x,LL y):x(x),y(y) {}
} a[maxn],ans[maxn];
int n;
Point operator -(Point a, Point b)
{
    return Point(a.x-b.x,a.y-b.y);
}
LL chaji(Point a,Point b)
{
    return a.x*b.y-a.y*b.x;
}
LL dis(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)
        return a.y<b.y;
    return a.x<b.x;
}
bool check(Point o,Point x,Point y)//交排序
{
    return chaji((x-o),(y-o))>0 ? 0:1;
}
int convex()
{
    int k=0;
    sort(a,a+n,cmp);
    for(int i=0; i<n; i++)
    {
        while(k>1&&check(ans[k-2],ans[k-1],a[i]))
            k--;
        ans[k++]=a[i];
    }
    int lim=k;
    for(int i=n-2; i>=0; i--)
    {
        while(k>lim&&check(ans[k-2],ans[k-1],a[i]))
            k--;
        ans[k++]=a[i];
    }
    if(n>1)
        k--;
    return k;
}
int main()
{
    LL now=2;
    LL MAXN=-1;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%lld%lld",&a[i].x,&a[i].y);
    int k=convex();
    for(int i=0; i<k; i++)
    {
       while(chaji((ans[i+1]-ans[i]),(ans[now+1]-ans[i])) > chaji((ans[i+1]-ans[i]),(ans[now]-ans[i])))
            now=(now+1)%k;
        MAXN=max(MAXN,dis(ans[i],ans[now]));
    }
    printf("%lld\n",MAXN);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdau_fangshifeng/article/details/89764805