pku 3587 The Biggest Cake

来源:互联网 发布:php函数插件 编辑:程序博客网 时间:2024/05/24 01:45

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

首先按照O(n^3)暴了一次,2000MS。然后按照O(n^2logn)写了一次,700MS.主要有一些计算几何的注意事项。

1。 sin , cos , atan ,tan ,sqrt 等数学函数执行的速度很慢,最好避免频繁使用

2. 关于精度。 爆的时候开了一个eps = 1e-10,.wa . 改1e-5。ac。继续改直接 == .ac。

3。关于极角排序,写了两个模板。一个是求倾斜角,一个是求叉积。事实证明。叉积要快上N+1倍。

4. 关于三角形外接圆。要知道公式。。。

5。尽可能少的调用函数,可以提高效率。


#include <stdio.h>#include <iostream>#include <math.h>#include <sstream>#include <algorithm>using namespace std;template<class T>inline string toStr(const T& v){ostringstream os;os<<v;return os.str();}#define FOR(i,s,e) for (int (i)=(s);(i)<(e);i++)#define DEBUG(a)     printf("%s = %s\n", #a, toStr(a).c_str())const double eps=1e-8;template<class T>inline T isqr(T v) {return v*v;}template<class T>inline int isgn(T v) {return (v>eps) - (v<-eps);}const double pi=acos(-1.0);struct Point2D {        double x, y;        Point2D():x(0), y(0){}        Point2D(const double& X, const double& Y):x(X), y(Y){}        //点按逆时针围绕坐标原点旋转alpha角度        void rotate(double alpha) {                double tx=x*cos(alpha)-y*sin(alpha);                double ty=y*cos(alpha)+x*sin(alpha);                x=tx; y=ty;        }        bool operator < (const Point2D& v) const {return y<v.y || !isgn(y-v.y) && x<v.x;}        bool operator ==(const Point2D& v) const {return !isgn(x-v.x) && !isgn(y-v.y);}        bool operator !=(const Point2D& v) const {return isgn(x-v.x) || isgn(y-v.y);}        Point2D operator +(const Point2D& v) const {return Point2D(x+v.x, y+v.y);}        Point2D operator -(const Point2D& v) const {return Point2D(x-v.x, y-v.y);}        template<class T>Point2D operator *(const T& v) const {return Point2D(x*v, y*v);}        template<class T>Point2D operator /(const T& v) const {return Point2D(x/v, y/v);}        Point2D& operator +=(const Point2D& v) {x+=v.x; y+=v.y; return *this;}        Point2D& operator -=(const Point2D& v) {x-=v.x; y-=v.y; return *this;}        template<class T>Point2D& operator *=(const T& v) {x*=v; y*=v; return *this;}        template<class T>Point2D& operator /=(const T& v) {x/=v; y/=v; return *this;}};struct NODE{    Point2D p;    int num;};//向量叉积os 与oedouble multiply(Point2D s,Point2D e,Point2D o){return sqrt((s.x-o.x)*(e.y-o.y)-(e.x-o.x)*(s.y-o.y));}// 返回两点之间欧氏距离double dist(Point2D p1,Point2D p2){return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );}Point2D p[700];double dis[700][700];NODE q[700];int center;bool cmp(const NODE & a,const NODE & b)                                                                                       //极角排序函数{     double temp;     temp=multiply(p[center],a.p,b.p);     if(temp>0)        return true;     else if(temp==0 && (dis[a.num][center]<dis[b.num][center]) )        return true;     return false;}double outCir(int i, int j, int k){    double a = dis[i][j];    double b = dis[i][k];    double c = dis[k][j];    double area= (p[j].x-p[i].x)*(p[k].y-p[i].y) - (p[j].y-p[i].y)*(p[k].x-p[i].x);    area=fabs(area)/2;    area = a * b * c /area/4;    return area;}int main(){// freopen("in.txt","r",stdin);    int t;    scanf("%d",&t);    int n;    double max,r;    while (t--)    {        scanf("%d",&n);        FOR(i,0,n)        {         scanf("%lf%lf",&p[i].x,&p[i].y);         q[i].p = p[i];         q[i].num = i;        }        max = -1;        FOR(i,0,n)        FOR(j,i+1,n)        dis[i][j] = dis[j][i] = dist(p[i],p[j]);        FOR(i,0,n)        {            center = i;            sort(q,q+n,cmp);            FOR(j,1,n-1)            {                r = outCir( center,q[j].num,q[j+1].num);               // DEBUG(r);                if (r - max > eps)                max = r;            }        }        printf("%.3lf\n",max);    }    return 0;}