POJ 3384 Feng Shui

这题又写抽了,目测SPJ没有Discuss里面说的那么坑吧。一遍非规范半平面交,然后求最远点对,用的是类似于旋转卡壳找对踵点的方法。一开始还很脑残地写成最大直径问题了……去……

#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 = 102;

typedef struct Point
{
    double o[2];
    double & operator [] (int i)
    {
        return o[i];
    }
    friend ostream & operator << (ostream & cout,Point argu)
    {
        return cout<<"("<<argu[0]<<","<<argu[1]<<")";
    }
    Point(double x=0,double 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 + (double argu)
    {
        return (*this)*(1+argu/length());
    }
    Point operator - (double argu)
    {
        return (*this)*(1-argu/length());
    }
    Point operator * (double argu)
    {
        return Point(o[0]*argu,o[1]*argu);
    }
    Point operator / (double 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
    {
        return cmp(o[0],argu[0])==0&&cmp(o[1],argu[1])==0;
    }
    double length(void)
    {
        return sqrt(o[0]*o[0]+o[1]*o[1]);
    }
    double angle(void)
    {
        return fmod(atan2(o[1],o[0])+2*pi,2*pi);
    }
}Vector;

double dot(Vector a,Vector b)
{
    return a[0]*b[0]+a[1]*b[1];
}
double 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);
}
double cross(Vector a,Vector b)
{
    return a[0]*b[1]-a[1]*b[0];
}
double 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 area(Point poly[],int n)
{
    double ans=0;
    for(int i=1;n>i+1;i++)
    {
        ans+=cross(poly[0],poly[i],poly[i+1]);
    }
    return abs(ans)/2;
}

int boundingBox(double x,double a,double 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;
}

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)
{
    int cnt[3]={0};
    poly[n]=poly[0];
    for(int i=0;n>i;i++)
    {
        cnt[side(p,poly[i],poly[i+1])+1]=true;
        if(cnt[1]) return 0;
        if(cnt[2]&&cnt[0]) 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;
    }
}

struct Line
{
    Point s,e;
    double o[3],angle;
    double & operator [] (int i)
    {
        return o[i];
    }
    friend ostream & operator << (ostream & cout,Line argu)
    {
        return cout<<"["<<argu.s<<"->"<<argu.e<<"]";
        return cout<<"["<<argu[0]<<","<<argu[1]<<" "<<argu[2]<<"]";
    }
    Line(Point s,Point e)
    {
        this->s=s;
        this->e=e;
        o[0]=s[1]-e[1];
        o[1]=e[0]-s[0];
        o[2]=cross(s,e);
        angle=(e-s).angle();
    }
    Line(double a=0,double b=0,double c=1)
    {
        o[0]=a;
        o[1]=b;
        o[2]=c;
        if(cmp(a,0)!=0)
        {
            s=Point(-c/a,0);
            e=s+dir();
        }
        else if(cmp(b,0)!=0)
        {
            s=Point(0,-c/b);
            e=s+dir();
        }
        angle=(e-s).angle();
    }
    Line operator + (double argu)
    {
        return Line(s+pen()*argu/pen().length(),e+pen()*argu/pen().length());
    }
    Line operator - (double argu)
    {
        return Line(s-pen()*argu/pen().length(),e-pen()*argu/pen().length());
    }
    bool operator < (Line argu) const
    {
        if(cmp(angle,argu.angle)!=0) return cmp(angle,argu.angle)<0;
        else return side(argu.s,s,e)<0;
    }
    bool operator == (Line argu) const
    {
        return cmp(angle,argu.angle)==0;
    }
    Vector dir(void) const
    {
        return Vector(o[1],-o[0]);
    }
    Vector pen(void) const
    {
        return Vector(o[0],+o[1]);
    }
};

int ward(Point p,Line l)
{
    return ward(p,l.s,l.e);
}
int side(Point p,Line l)
{
    return side(p,l.s,l.e);
}

#ifndef SPECIAL
#define SPECIAL

Point solve(double a1,double b1,double c1,double a2,double b2,double c2)
{
    Point A(a1,a2);
    Point B(b1,b2);
    Point R(c1,c2);
    return Point(cross(R,B),cross(A,R))/cross(B,A);
}
Point meet(Line a,Line b)
{
    return solve(a[0],a[1],a[2],
                 b[0],b[1],b[2]);
}
Point foot(Point p,Line l)
{
    return solve(l[0],l[1],l[2],
                 l[1],-l[0],l[0]*p[1]-l[1]*p[0]);
}
Point symmetry(Point p,Line l)
{
    return solve(l[0],l[1],l[0]*p[0]+l[1]*p[1]+2*l[2],
                 l[1],-l[0],l[0]*p[1]-l[1]*p[0]);
}

#endif // SPECIAL

#ifndef DIS
#define DIS

double dis(Point a,Point b)
{
    return (b-a).length();
}
double disPar(Point p,Line l)
{
    return dot(l.s,l.e,p)/dis(l.s,l.e);
}
double disPen(Point p,Line l)
{
    return cross(l.s,l.e,p)/dis(l.s,l.e);
}
double dis(Point p,Point s,Point e)
{
    if(ward(p,s,e)*ward(p,e,s)>0) return disPen(p,Line(s,e));
    else return fmin(dis(p,s),dis(p,e));
}
double dis(Line a,Line b)
{
    if(cmp(a.angle,b.angle)!=0) return 0;
    else return disPen(a.s,b);
}
double dis(Line l,Point s,Point e)
{
    if(side(s,l)*side(e,l)<0) return 0;
    else return fmin(disPen(s,l),disPen(e,l));
}
double dis(Point s1,Point e1,Point s2,Point e2)
{
    if(intersection(s1,e1,s2,e2)>=0) return 0;
    else return fmin(fmin(dis(s1,s2,e2),dis(e1,s2,e2)),fmin(dis(s2,s1,e1),dis(e2,s1,e1)));
}

#endif // DIS

int n;
double r,x,y;
Line l[MAXV],plane[MAXV];
Point p[MAXV],convex[MAXV];

int HPI(Line l[],int n)
{
    int f=0,r=-1,cnt=0;
    n=(sort(l,l+n),unique(l,l+n))-l;

    for(int i=0;n>i;i++)
    {
        while(f<r&&side(meet(plane[r-1],plane[r]),l[i])<0) r--;
        while(f<r&&side(meet(plane[f+1],plane[f]),l[i])<0) f++;
        plane[++r]=l[i];
    }
    while(f<r&&side(meet(plane[r-1],plane[r]),plane[f])<0) r--;
    while(f<r&&side(meet(plane[f+1],plane[f]),plane[r])<0) f++;

    for(int i=f;i<r;i++) convex[cnt++]=meet(plane[i],plane[i+1]);
    convex[cnt++]=meet(plane[f],plane[r]);
    return cnt;
}
Line rotateCliper(Point poly[],int n)
{
    int f=1;
    Point s=poly[0],e=poly[0];

    p[n]=p[0];
    for(int i=0;n>i;i++)
    {
        while(cmp(dis(poly[i],poly[f+1]),dis(poly[i],poly[f]))>0) f=(f+1)%n;
        if(cmp(dis(poly[i],poly[f]),dis(s,e))>=0)
        {
            s=poly[i];
            e=poly[f];
        }
    }
    return Line(s,e);
}

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

    while(scanf("%d %lf",&n,&r)!=EOF)
    {
        for(int i=0;n>i;i++)
        {
            scanf("%lf %lf",&x,&y);
            p[i]=Point(x,y);
        }
        for(int i=0;n>i;i++)
        {
            l[i]=Line(p[(i+1)%n],p[i])+r;
        }
        Line ans=rotateCliper(convex,HPI(l,n));
        printf("%.6f %.6f %.6f %.6f\n",ans.s[0],ans.s[1],ans.e[0],ans.e[1]);
    }

    return 0;
}

你可能感兴趣的:(POJ 3384 Feng Shui)