2618: [Cqoi2006]凸多边形

Time Limit: 5 Sec Memory Limit: 128 MB

Description

逆时针给出n个凸多边形的顶点坐标,求它们交的面积。例如n=2时,两个凸多边形如下图:



则相交部分的面积为5.233。

Input

第一行有一个整数n,表示凸多边形的个数,以下依次描述各个多边形。第i个多边形的第一行包含一个整数mi,表示多边形的边数,以下mi行每行两个整数,逆时针给出各个顶点的坐标。

Output

输出文件仅包含一个实数,表示相交部分的面积,保留三位小数。

Sample Input

2

6

-2 0

-1 -2

1 -2

2 0

1 2

-1 2

4

0 -3

1 -1

2 2

-1 0

Sample Output

5.233

HINT

100%的数据满足:2<=n<=10,3<=mi<=50,每维坐标为[-1000,1000]内的整数

这是一道半平面交模板题,实际上,这道题将半平面交的裸板题中的一个多边形拆成了多个多边形,这样的话只需要在输入时调整一下就可以套上板子了(然而本蒟蒻调输入调了30min+" role="presentation" style="position: relative;">30min+30min+),代码实现上也没有什么太多的细节,简单用双端队列维护一下就行了。

代码如下:

#include<bits/stdc++.h>
#define N 1005
#define eps 1.0e-12
using namespace std;
struct point{double x,y;}p[N];
struct line{
    point a,b;
    double poa;
}l[N];
int n,m,tot,head=0,tail=0,q[N],siz=0;
inline point operator-(point a,point b){return point{a.x-b.x,a.y-b.y};}
inline double cross(point a,point b){return a.x*b.y-a.y*b.x;}
inline point across(line a,line b){
    double a1=cross(b.b-a.a,b.a-a.a),a2=cross(b.a-a.b,b.b-a.b);
    return point{(a2*a.a.x+a1*a.b.x)/(a2+a1),(a2*a.a.y+a1*a.b.y)/(a2+a1)};
}
inline bool check(point a,line b){return cross(a-b.a,b.b-b.a)>0;}
inline bool cmp(line a,line b){
    if(fabs(a.poa-b.poa)<eps)return cross(a.b-b.a,b.b-b.a)<0;
    return a.poa<b.poa;
}
inline double solve(){
    sort(l+1,l+tot+1,cmp);
    for(int i=1;i<=tot;++i)if(fabs(l[i-1].poa-l[i].poa)>eps)++siz,l[siz]=l[i];
    q[1]=head=tail=1;
    for(int i=2;i<=siz;++i){
        while(head<tail&&check(across(l[q[tail-1]],l[q[tail]]),l[i]))--tail;
        while(head<tail&&check(across(l[q[head]],l[q[head+1]]),l[i]))++head;
        q[++tail]=i;
    }
    while(head<tail&&check(across(l[q[tail-1]],l[q[tail]]),l[q[head]]))--tail;
    while(head<tail&&check(across(l[q[head]],l[q[head+1]]),l[q[tail]]))++head;
    if(tail-head<=1)return 0.000;
    for(int i=head;i<tail;++i)p[i-head+1]=across(l[q[i]],l[q[i+1]]);
    p[tail-head+1]=across(l[q[tail]],l[q[head]]);
    double ans=0.0000;
    for(int i=1;i<=tail-head;++i)ans+=cross(p[i],p[i+1]);
    ans+=cross(p[tail-head+1],p[1]);
    return ans/2.0;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d",&m);
        for(int j=1;j<=m;++j)scanf("%lf%lf",&p[j].x,&p[j].y);
        p[m+1]=p[1];
        for(int j=1;j<=m;++j)++tot,l[tot].a=p[j],l[tot].b=p[j+1];
    }
    for(int i=1;i<=tot;++i)l[i].poa=atan2(l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);
    printf("%.3lf",solve());
    return 0;
}

2018.07.04 BZOJ 2618 Cqoi2006凸多边形(半平面交)的更多相关文章

  1. bzoj 2618: [Cqoi2006]凸多边形 [半平面交]

    2618: [Cqoi2006]凸多边形 半平面交 注意一开始多边形边界不要太大... #include <iostream> #include <cstdio> #inclu ...

  2. 洛谷 P4196 [CQOI2006]凸多边形 (半平面交)

    题目链接:P4196 [CQOI2006]凸多边形 题意 给定 \(n\) 个凸多边形,求它们相交的面积. 思路 半平面交 半平面交的模板题. 代码 #include <bits/stdc++. ...

  3. 2018.07.03 POJ 1279Art Gallery(半平面交)

    Art Gallery Time Limit: 1000MS Memory Limit: 10000K Description The art galleries of the new and ver ...

  4. ●BZOJ 2618 [Cqoi2006]凸多边形

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2618 题解: 计算几何,半平面交. 给出一些凸包,求面积交. 把所有边都取出来,直接办平面交 ...

  5. 2018.07.04 BZOJ 2823: AHOI2012信号塔(最小圆覆盖)

    2823: [AHOI2012]信号塔 Time Limit: 10 Sec Memory Limit: 128 MB Description 在野外训练中,为了确保每位参加集训的成员安全,实时的掌握 ...

  6. 【BZOJ 2618】 2618: [Cqoi2006]凸多边形 (半平面交)

    2618: [Cqoi2006]凸多边形 Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input 第一 ...

  7. bzoj 2618 2618: [Cqoi2006]凸多边形(半平面交)

    2618: [Cqoi2006]凸多边形 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 656  Solved: 340[Submit][Status] ...

  8. BZOJ - 2618 凸多边形 (半平面交)

    题意:求n个凸多边形的交面积. 半平面交模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; ty ...

  9. 2018.07.03 BZOJ 1007: [HNOI2008]水平可见直线(简单计算几何)

    1007: [HNOI2008]水平可见直线 Time Limit: 1 Sec Memory Limit: 162 MB Description 在xoy直角坐标平面上有n条直线L1,L2,-Ln, ...

随机推荐

  1. delphi 加密 XOR

    From  http://www.delphigeist.com/2009/09/text-encryption-with-xor.html Text encryption with XOR   Ev ...

  2. net send 换行和发送广播

    net send ip message 要换行的时候按ctrl+t.最后按enter 或 ctrl + m 发出 如果是批处理里面要用: 在命令行下使用:echo ^T > a.txt,注意这里 ...

  3. CentOS 7 JDK安装

    官网:  http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.下载(下图有个错误 ...

  4. 通过sizeof获得数组长度的方法

    int a[20]; int len = sizeof(a)/sizeof(*a); //值为20,即数组长度为20 注:sizeof是一个操作符,sizeof的值在编译时确定

  5. Linux系统之更改默认块大小

    查看操作系统块大小:#tune2fs  -l /dev/sda1 |grep 'Block size'               ( tune2fs  -l  /dev/sda1可以查看更多相关文件 ...

  6. 【342】Linear Regression by Python

    Reference: 用scikit-learn和pandas学习线性回归 首先获取数据存储在 pandas.DataFrame 中,获取途径(CSV 文件.Numpy 创建) 将数据分成 X 和 y ...

  7. 吴裕雄 python神经网络 水果图片识别(2)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom skimage import color,data,transform,i ...

  8. ionic 2,带着运气成分

    npm config set loglevel info   查看安装信息 npm cache clean                  清除缓存 cnpm sync ionic         ...

  9. samtools 的应用

    1)sam转bam samtools view -bS in.sam > in.bam -b 意思使输出使BAM format -S 意思使输入使SAM,如果@SQ 缺剩, 要写-t

  10. HttpURLConnection(二)

    package com.cmy.urlcon; import java.io.BufferedReader; import java.io.InputStream; import java.io.In ...