现在的位置: 首页 > 综合 > 正文

POJ 2187 Beauty Contest

2014年01月16日 ⁄ 综合 ⁄ 共 4634字 ⁄ 字号 评论关闭

一遍规范凸包,一遍旋转卡壳求最远点对之间的距离平方。

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>

using namespace std;

#pragma pack(4)

const double  eps = 1e-8;
const double   pi = acos(-1.0);
const int     inf = 0x7f7f7f7f;

#define loop(a,n)                            \
    for(int i=0;n>i;i++)                     \
        cout<<a[i]<<(i!=n-1?' ':'\n')
#define loop2(a,n,m)                         \
    for(int i=0;n>i;i++)                     \
        for(int j=0;m>j;j++)                 \
            cout<<a[i][j]<<(j!=m-1?' ':'\n')

#define   at(a,i) ((a)&(1<<(i)))
#define   nt(a,i) ((a)^(1<<(i)))
#define set1(a,i) ((a)|(1<<(i)))
#define set0(a,i) ((a)&(~(1<<(i))))

#define cmp(a,b) (fabs((a)-(b))<eps?0:(((a)-(b))>eps?+1:-1))

#define lmax(a,b) ((a)>(b)?(a):(b))
#define lmin(a,b) ((a)<(b)?(a):(b))
#define fmax(a,b) (cmp(a,b)>0?(a):(b))
#define fmin(a,b) (cmp(a,b)<0?(a):(b))

const int MAXV = 50002;

typedef struct Point
{
    int o[2];
    int & operator [] (int i)
    {
        return o[i];
    }
    friend istream & operator >> (istream & cin,Point & argu)
    {
        return cin>>argu[0]>>argu[1];
    }
    friend ostream & operator << (ostream & cout,Point argu)
    {
        return cout<<"("<<argu[0]<<","<<argu[1]<<")";
    }
    Point(int x=0,int y=0)
    {
        o[0]=x;
        o[1]=y;
    }
    Point operator + (Point argu)
    {
        return Point(o[0]+argu[0],o[1]+argu[1]);
    }
    Point operator - (Point argu)
    {
        return Point(o[0]-argu[0],o[1]-argu[1]);
    }
    Point operator + (int argu)
    {
        return (*this)*(1+argu/length());
    }
    Point operator - (int argu)
    {
        return (*this)*(1-argu/length());
    }
    Point operator * (int argu)
    {
        return Point(o[0]*argu,o[1]*argu);
    }
    Point operator / (int argu)
    {
        return Point(o[0]/argu,o[1]/argu);
    }
    bool operator > (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])>0;
        else return cmp(o[1],argu[1])>0;
    }
    bool operator >= (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])>=0;
        else return cmp(o[1],argu[1])>=0;
    }
    bool operator < (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])<0;
        else return cmp(o[1],argu[1])<0;
    }
    bool operator <= (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])<=0;
        else return cmp(o[1],argu[1])<=0;
    }
    bool operator != (Point argu) const
    {
        return cmp(o[0],argu[0])!=0||cmp(o[1],argu[1])!=0;
    }
    bool operator == (Point argu) const
    {
        return cmp(o[0],argu[0])==0&&cmp(o[1],argu[1])==0;
    }
    int length(void) const
    {
        return o[0]*o[0]+o[1]*o[1];
    }
    int angle(void) const
    {
        return fmod(atan2(o[1],o[0])+2*pi,2*pi);
    }
}Vector;

int dot(Vector a,Vector b)
{
    return a[0]*b[0]+a[1]*b[1];
}
int dot(Point o,Point a,Point b)
{
    return dot(a-o,b-o);
}
int ward(Point p,Point s,Point e)
{
    return cmp(dot(s,e,p),0);
}
int cross(Vector a,Vector b)
{
    return a[0]*b[1]-a[1]*b[0];
}
int cross(Point o,Point a,Point b)
{
    return cross(a-o,b-o);
}
int side(Point p,Point s,Point e)
{
    return cmp(cross(s,e,p),0);
}

double angle(Vector a,Vector b)
{
    return (b-a).length();
}
double angle(Point o,Point a,Point b)
{
    return angle(a-o,b-o);
}

int boundingBox(int x,int a,int b)
{
    return cmp(a,x)*cmp(x,b);
}
int boundingBox(Point p,Point s,Point e)
{
    if(boundingBox(p[0],s[0],e[0])>0&&boundingBox(p[1],s[1],e[1])>0) return 1;
    else if(boundingBox(p[0],s[0],e[0])>=0&&boundingBox(p[1],s[1],e[1])>=0) return 0;
    else return -1;
}

Point solve(int a,int b,int c,int d,int e,int f)
{
    Point A(a,b);
    Point B(c,d);
    Point R(e,f);
    return Point(cross(B,R),cross(R,A))/cross(A,B);
}

int locate(Point p,Point s,Point e)
{
    if(p==s||p==e) return 0;
    return side(p,s,e)==0&&boundingBox(p,s,e)>=0?1:-1;
}
int locate(Point p,Point poly[],int n)
{
    poly[n]=poly[0];
    map<int,bool> cnt;
    for(int i=0;n>i;i++)
    {
        cnt[side(p,poly[i],poly[i+1])]=true;
        if(cnt[0]) return 0;
        if(cnt[-1]&&cnt[1]) return -1;
    }
    return 1;
}

int intersection(Point s1,Point e1,Point s2,Point e2)
{
    if(side(s1,s2,e2)*side(e1,s2,e2)<0&&side(s2,s1,e1)*side(e2,s1,e1)<0) return 1;
    else
    {
        if(locate(s1,s2,e2)>=0) return 0;
        if(locate(e1,s2,e2)>=0) return 0;
        if(locate(s2,s1,e1)>=0) return 0;
        if(locate(e2,s1,e1)>=0) return 0;
        return -1;
    }
}

int area(Point poly[],int n)
{
    int ans=0;
    for(int i=1;n>i+1;i++)
    {
        ans+=cross(poly[0],poly[i],poly[i+1]);
    }
    return abs(ans)/2;
}

Point convex[MAXV];

int convexHull(Point poly[],int n)
{
    int cnt=0,temp=0;
    sort(poly,poly+n);
    for(int i=0;n>i;i++)
    {
        while(cnt>=temp+2&&side(poly[i],convex[cnt-2],convex[cnt-1])<=0)
            cnt--;
        convex[cnt++]=poly[i];
    }
    temp=cnt-1;
    for(int i=n-2;i>=0;i--)
    {
        while(cnt>=temp+2&&side(poly[i],convex[cnt-2],convex[cnt-1])<=0)
            cnt--;
        convex[cnt++]=poly[i];
    }
    return cnt-1;
}
int RC(Point poly[],int n)
{
    int f=1,ans=0;
    poly[n]=poly[0];
    for(int i=0;n>i;i++)
    {
        while(cmp(cross(poly[i],poly[i+1],poly[f+1]),cross(poly[i],poly[i+1],poly[f]))>0)
            f=(f+1)%n;
        ans=max(ans,(poly[i]-poly[f]).length());
    }
    return ans;
}

int n,x,y;
Point poly[MAXV];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("Beauty Contest.txt","r",stdin);
    #else
    #endif

    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;n>i;i++)
        {
            scanf("%d %d",&x,&y);
            poly[i]=Point(x,y);
        }
        printf("%d\n",RC(convex,convexHull(poly,n)));
    }

    return 0;
}

抱歉!评论已关闭.