poj 3743

给你一个圆,切了n条直线,问切成块的最大面积是多少,保证没有三线共点。

学到了很多!
其实牛客多校是我第一次听说这东西,
之前一直误以为pslg不好处理三线共点的情况,发现自己简直太傻逼了,完全可以求出来所有点然后按直线加边。

这道题主要就在于如何求圆弧吧,
我们不妨把圆弧上的点按极角排序,记录一下每个点的后继,
在pslg求面积的时候,我们直接判两个点是否都在圆上以及他们是否是圆弧上相邻的两个点。

但是这样会遇到很恐怖的事情,注意到我们上面求的其实都是 多边形+弓形区域的面积。
如果区域就是一块弓形呢?那我们就会错误地把 分开的 弓形和多边形 加到一起,
所以我们对 圆弧上的每两个相邻的点取中点再连边,这样就能保证每个弓形都是属于一个多边形的了

挺巧妙的,吃了 5发ce 1发mle 1发tle,
再写poj的题我就是傻逼。
tle了记得加inline,能从tle优化到700ms,

大概再写几道plsg,要去写写voronoi图了,
一直以为voronoi图比较冷门,反正我只知道05年wf出了一个,,,今晚跟队友讨论的时候,
提到了17年wf,他说熊猫吃竹子也是voronoi图,,惊了。。
说起来这题都放置一年多了都没补。。。
呜呜呜萌新太惨了

#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <assert.h>
#include <cstdio>
#define yxn inline
#define INF 19970404
using namespace std;
typedef double db;
const int maxn = 1e5+5;
const db eps=1e-6;
const db pi=acos(-1);
yxn int sign(db k){
    if (k>eps) return 1; else if (k<-eps) return -1; return 0;
}
yxn int cmp(db k1,db k2){return sign(k1-k2);}
yxn int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内
struct point{
    db x,y;int id,nxt;
    //point (db x,db y,int id=0,int nxt=0){};
    point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
    point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
    point operator * (db k1) const{return (point){x*k1,y*k1};}
    point operator / (db k1) const{return (point){x/k1,y/k1};}
    int operator == (const point &k1) const{return cmp(x,k1.x)==0&&cmp(y,k1.y)==0;}
    // 逆时针旋转
    point turn(db k1){return (point){x*cos(k1)-y*sin(k1),x*sin(k1)+y*cos(k1)};}
    point turn90(){return (point){-y,x};}
    bool operator < (const point k1) const{
        int a=cmp(x,k1.x);
        if (a==-1) return 1; else if (a==1) return 0; else return cmp(y,k1.y)==-1;
    }
    db abs(){return sqrt(x*x+y*y);}
    db abs2(){return x*x+y*y;}
    db dis(point k1){return ((*this)-k1).abs();}
    point unit(){db w=abs(); return (point){x/w,y/w};}
    void scan(){double k1,k2; scanf("%lf%lf",&k1,&k2); x=k1; y=k2;}
    void print(){printf("%.11lf %.11lf\n",x,y);}
    db getw(){return atan2(y,x);}
    point getdel(){if (sign(x)==-1||(sign(x)==0&&sign(y)==-1)) return (*this)*(-1); else return (*this);}
    int getP() const{return sign(y)==1||(sign(y)==0&&sign(x)==-1);}
};
yxn int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);}
yxn db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;}
yxn db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;}
yxn db rad(point k1,point k2){return atan2(cross(k1,k2),dot(k1,k2));}
// -pi -> pi
yxn int compareangle (point k1,point k2){//极角排序+
    return k1.getP()<k2.getP()||(k1.getP()==k2.getP()&&sign(cross(k1,k2))>0);
}
yxn point getLL(point k1,point k2,point k3,point k4){
    db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3); return (k1*w2+k2*w1)/(w1+w2);
}
yxn int intersect(db l1,db r1,db l2,db r2){
    if (l1>r1) swap(l1,r1); if (l2>r2) swap(l2,r2); return cmp(r1,l2)!=-1&&cmp(r2,l1)!=-1;
}
yxn int onS(point k1,point k2,point q){return inmid(k1,k2,q)&&sign(cross(k1-q,k2-k1))==0;}
struct circle{
    point o; db r;
    void scan(){o.scan(); scanf("%lf",&r);}
    int inside(point k){return cmp(r,o.dis(k));}
};
struct line{
    // p[0]->p[1]
    point p[2];
    line(point k1,point k2){p[0]=k1; p[1]=k2;}
    line(){}
    point& operator [] (int k){return p[k];}
    int include(point k){return sign(cross(p[1]-p[0],k-p[0]))>0;}
    point dir(){return p[1]-p[0];}
    line push(){ // 向外 ( 左手边 ) 平移 eps
        const db eps = 1e-6;
        point delta=(p[1]-p[0]).turn90().unit()*eps;
        return {p[0]-delta,p[1]-delta};
    }
};
yxn point getLL(line k1,line k2){return getLL(k1[0],k1[1],k2[0],k2[1]);}
yxn int parallel(line k1,line k2){return sign(cross(k1.dir(),k2.dir()))==0;}
yxn int sameDir(line k1,line k2){return parallel(k1,k2)&&sign(dot(k1.dir(),k2.dir()))==1;}
yxn int operator < (line k1,line k2){
    if (sameDir(k1,k2)) return k2.include(k1[0]);
    return compareangle(k1.dir(),k2.dir());
}
yxn int checkpos(line k1,line k2,line k3){return k3.include(getLL(k1,k2));}

yxn db leaf(point a,point b,db r=10){
    db j = abs(rad(a,b));
    j = min(j,pi-j);
    db s = 0.5*r*r*(j-sin(j));
    return s;
}
yxn db Area(vector<point>& A){
    db ans=0;
    for (int i=0;i<A.size();i++) ans+=cross(A[i],A[(i+1)%A.size()]);
    ans/=2;
    for(int i=0;i<A.size();i++){
        if(cmp(A[i].abs(),10)==0&&cmp(A[(i+1)%A.size()].abs(),10)==0&&A[i].nxt==A[(i+1)%A.size()].id){
            ans+=leaf(A[i],A[(i+1)%A.size()]);
        }
    }
    return ans;
}
typedef vector<point> Polygon;
struct Edge{
    int from,to;
    db ang;
    Edge(int f,int t,double a):from(f),to(t),ang(a){}
};
struct PSLG{
    int n,m,face_cnt;//face_cnt 面数
    point p[maxn];
    vector<Edge>edges;//储存边
    vector<int>G[maxn];//指向边
    int vis[maxn*2];  // 每条边是否已经访问过
    int left[maxn*2]; // 左面的编号
    int prev[maxn*2]; // 相同起点的上一条边(即顺时针旋转碰到的下一条边)的编号
    vector<Polygon> faces;//faces 储存面
    double area[maxn]; //每个polygon的面积
    void init(int n){
        this->n = n;
        for(int i=0;i<n;i++)
            G[i].clear();
        edges.clear();
        faces.clear();
    }
    //from->to的极角
    double getAngle(int from, int to){
        return atan2(p[to].y-p[from].y, p[to].x-p[from].x);
    }
    void AddEdge(int from, int to){
        edges.push_back((Edge){from, to, getAngle(from, to)});
        edges.push_back((Edge){to, from, getAngle(to, from)});
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    // 找出faces并计算面积
    void Build(){
        for(int u = 0; u < n; u++){
            // 给从u出发的各条边按极角排序
            int d = G[u].size();
            for(int i = 0; i < d; i++)//wtmd。。。。
                for(int j = i+1; j < d; j++)
                    if(edges[G[u][i]].ang > edges[G[u][j]].ang)
                        swap(G[u][i], G[u][j]);

            for(int i = 0; i < d; i++)
                prev[G[u][(i+1)%d]] = G[u][i];
        }
        memset(vis, 0, sizeof(vis));
        face_cnt = 0;
        for(int u = 0; u < n; u++)
            for(int i = 0; i < G[u].size(); i++){
                int e = G[u][i];
                if(!vis[e]){// 逆时针找圈
                    face_cnt++;
                    Polygon poly;
                    for(;;){
                        vis[e] = 1;
                        left[e] = face_cnt;
                        int from = edges[e].from;
                        poly.push_back(p[from]);
                        e = prev[e^1];
                        if(e == G[u][i])
                            break;
                        assert(vis[e] == 0);
                    }
                    faces.push_back(poly);
                }
            }

        for(int i = 0; i < faces.size(); i++){
            area[i] = Area(faces[i]);
        }
    }
};

#define pdp pair<db,point>
#define ppi pair<point,int>
int t,n;
PSLG pslg;
pdp a[maxn];
line l[maxn];
vector<int>v[maxn];
point x[maxn];
ppi st[maxn];
yxn bool cmp2(int a,int b){return x[a]<x[b];}
yxn bool cmp3(ppi a,ppi b){return compareangle(a.first,b.first);}
yxn void insert(line *seg,int m){
    int cnt=0;
    for(int i=0;i<m;i++){
        for(int j=i+1;j<m;j++){
            if(sameDir(seg[i],seg[j]))continue;
            point tmp = getLL(seg[i],seg[j]);
            if(cmp(tmp.abs(),10.0)>0)continue;
            x[cnt++]=tmp;
        }
    }
    sort(x,x+cnt);
    cnt=unique(x,x+cnt)-x;
    int ed=0;
    for(int i=0;i<cnt;i++){
        if(cmp(x[i].abs(),10)==0){
            st[ed++]=ppi(x[i],i);
        }
    }
    sort(st,st+ed,cmp3);
    for(int i=0;i<cnt;i++)x[i].id=i;
    for(int i=0;i<ed;i++)x[st[i].second].nxt=x[st[(i+1)%cnt].second].id;
    pslg.init(cnt);
    for(int i=0;i<cnt;i++){
        pslg.p[i]=x[i];
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<cnt;j++){
            if(onS(seg[i][0],seg[i][1],x[j])){
                v[i].push_back(j);
            }
        }
        sort(v[i].begin(),v[i].end(),cmp2);
        for(int j=0;j<v[i].size()-1;j++)pslg.AddEdge(v[i][j+1],v[i][j]);
    }
    pslg.Build();
    for(int i=0;i<m;i++)v[i].clear();
}
int main(){
    scanf("%d",&t);
    while (t--){
        scanf("%d",&n);
        int m=0,k=0;
        for(int i=1;i<=n;i++){
            db x,y;
            scanf("%lf%lf",&x,&y);
            point p1 = {cos(x)*10,sin(x)*10};
            point p2 = {cos(y)*10,sin(y)*10};
            a[k++]=pdp(x,p1);
            a[k++]=pdp(y,p2);
            l[m++]=line(p1,p2);
        }
        sort(a,a+k);
        k=unique(a,a+k)-a;
        a[k]=a[0];a[k].first+=2*pi;
        for(int i=0;i<k;i++){
            point tmp;
            if(cmp(a[i+1].first-a[i].first,pi)==0){
                tmp = a[i].second.turn90();
            }else if(cmp(a[i+1].first-a[i].first,pi)>0){
                tmp = (a[i].second+a[i+1].second).unit()*-10;
            }else {
                tmp = (a[i].second+a[i+1].second).unit()*10;
            }
            l[m++] = line(a[i].second,tmp);
            l[m++] = line(tmp,a[i+1].second);
        }
        insert(l,m);
        sort(pslg.area,pslg.area+pslg.face_cnt);
        printf("%.2f\n",pslg.area[pslg.face_cnt-1]);
    }
}
上一篇:redis


下一篇:golang Map