2618: [Cqoi2006]凸多边形

半平面交裸题,即便如此本辣鸡还是调试了一上午…
因为是求所有图形的交,那么直接把每个图形都拆成一堆直线,
相当于每次切一刀,留下左边的,然后求最后面积

c++代码如下:

#include<bits/stdc++.h>
#define eps 1e-10
#define rep(i,x,y) for(register int i = x ; i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
typedef long long ll;
using namespace std;
template<typename T>inline void read(T&x)
{
    x = 0;char c;int sign = 1;
    do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
    do { x = x * 10 + c - '0';  c = getchar(); }while(isdigit(c));
    x *= sign;
}

const int N = 501;
struct Point
{
    double x,y;
    inline void r() { read(x); read(y); }
}lst,pst,now,a[N];

struct Line
{
    double x,y;Point s;
    double ang;
    Line(){}
    Line(double a,double b) { x = a ; y = b; ang = atan2(y,x); }
    bool operator < (Line b) const { return ang < b.ang; }
}L[N],Q[N];

inline Line operator - (Point a,Point b) { return Line(a.x - b.x,a.y - b.y); }
inline double cross(Line x,Line y) { return x.x * y.y - x.y * y.x; }
inline bool OnRight(Line x,Point y) { return cross(x,y - x.s) < eps; }
inline Point Get(Line a,Line b)
{
    Point c;
    if(fabs(b.x) < eps || fabs(a.x) < eps)
    {
        if(fabs(b.x) < eps) swap(a,b);
        c.x = a.s.x; c.y = b.y *a.s.x/ b.x + (b.s.y - b.y*b.s.x / b.x) ;
        return c;
    }

    double a1 = a.y / a.x,a2 = b.y/b.x;
    double b1 = a.s.y - a.y * a.s.x / a.x,b2 = b.s.y - b.y*b.s.x/b.x;
    c.x = (b2 - b1) / (a1 - a2);
    c.y = c.x * a.y / a.x + b1;

    return c;
}

int n,cnt,st,en;

int main()
{
//  freopen("1.in","r",stdin);
//  freopen("my.txt","w",stdout);
    read(n);
    rep(i,1,n)
    {
        int num;
        read(num);
        lst.r();
        pst = lst;
        rep(i,2,num)
        {
            now.r();
            L[++cnt] = now - lst;
            L[cnt].s = lst;
            lst = now;
        }
        L[++cnt] = pst - lst;
        L[cnt].s = lst;
    }

    sort(L + 1,L + 1 + cnt);

    Q[st = en = 1] = L[st];

    rep(i,2,cnt)
    {
        while(en - st > 0 && OnRight(L[i],a[en - 1])) --en;
        while(en - st > 0 && OnRight(L[i],a[st])) ++st;
        Q[++en] = L[i];
        if(fabs(cross(Q[en],Q[en - 1])) < eps)
        {
            --en ;
            if(!OnRight(Q[en],L[i].s)) Q[en] = L[i];
        }
        if(en - st > 0) a[en - 1] = Get(Q[en - 1],Q[en]);
    }
    while(en - st > 0 && OnRight(Q[st],a[en - 1]))  --en;
    if(en - st <= 1) return puts("0.000"),0;
    a[en] = Get(Q[en],Q[st]);
    double ans = 0;
    rep(i,st + 1,en - 1) ans += fabs(cross((a[i] - a[st]), (a[i + 1] - a[st])));
    printf("%.3lf",ans/2+eps);
    return 0;
}
/*
2
3 1 1 2 2 1 2
3 1 1 0 0 0 1
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值