2618: [Cqoi2006]凸多边形

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]内的整数

半平面交模板:(终于缩到100行以内了。。。之前没删调试恶心的180+)

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define Maxn 1100 struct P
{
double x,y;
P() {x=y=;}
P(double x,double y):x(x),y(y){}
friend P operator - (P x,P y) {return P(x.x-y.x,x.y-y.y);}
friend P operator + (P x,P y) {return P(x.x+y.x,x.y+y.y);}
friend P operator * (P x,double y) {return P(x.x*y,x.y*y);}
friend double operator * (P x,P y) {return x.x*y.y-x.y*y.x;}
friend double operator / (P x,P y) {return x.x*y.x+x.y*y.y;}
}a[Maxn];
struct L
{
P a,b,v;double slop;
friend bool operator < (L a,L b) {return (a.slop!=b.slop)?(a.slop<b.slop):a.v*(b.b-a.a)>;}
friend P inter(L a,L b)
{
P nw=b.a-a.a;
double tt=(nw*a.v)/(a.v*b.v);
return b.a+b.v*tt;
}
friend bool jud(P x,L c) {return c.v*(x-c.a)<;}
}l[Maxn],q[Maxn];int cnt,tot; void ffind()
{
for(int i=;i<=cnt;i++) l[i].v=l[i].b-l[i].a,l[i].slop=atan2(l[i].v.y,l[i].v.x);
sort(l+,l++cnt);
int L=,R=;
tot=;
for(int i=;i<=cnt;i++)
{
if(l[i].slop!=l[i-].slop) tot++;
l[tot]=l[i];
}
cnt=tot;tot=;
q[++R]=l[];q[++R]=l[];
for(int i=;i<=cnt;i++)
{
while(L<R&&jud(inter(q[R-],q[R]),l[i])) R--;
while(L<R&&jud(inter(q[L+],q[L]),l[i])) L++;
q[++R]=l[i];
}
while(L<R&&jud(inter(q[R-],q[R]),q[L])) R--;
while(L<R&&jud(inter(q[L+],q[L]),q[R])) L++;
q[R+]=q[L];
for(int i=L;i<=R;i++) a[++tot]=inter(q[i],q[i+]);
} void init()
{
int n;
scanf("%d",&n);
cnt=;
for(int i=;i<=n;i++)
{
int m;
scanf("%d",&m);
P ft,now,nw;
scanf("%lf%lf",&ft.x,&ft.y);
now=ft;
for(int j=;j<=m;j++)
{
scanf("%lf%lf",&nw.x,&nw.y);
l[++cnt].b=nw,l[cnt].a=now;
now=nw;
}
l[++cnt].a=now;l[cnt].b=ft;
}
} void get_area()
{
double ans=;
for(int i=;i<tot;i++) ans+=a[i]*a[i+];
ans+=a[tot]*a[];
if(tot<) ans=;
printf("%.3lf\n",ans/);
} int main()
{
init();
ffind();
get_area();
return ;
}

【分析】

  然而只是想做一道半平面交的模版题,就从星期二打到了现在。。。【下午还要考试呢真是无爱。。

  这题是求凸包的交,我们可以把每一条线段转化半平面,求半平面交。

  对于半平面交,最朴素的想法应该是两两线段求交点,然后判断是否在每一个平面内,然后求凸包吧(感觉奇慢无比啊)

  而事实上,如果有n的半平面的话,半平面交的答案那个凸包不会超过n条边,因为每个半平面最多只贡献一条边,说明我们事实上做了很多很多无用功。

  根据凸包的思想,我们觉得半平面交也是有单调性的。

  那个nlogn的算法可以看zzy的论文《半平面交的新算法及其实用价值》。

  半平面共用向量表示,向量的左边为有效半平面。

  定义半平面的极角为表示半平面的向量的极角。

  根据半平面的极角进行排序,若两个半平面极角相同,明显只需要保存最靠左的半平面,根据这个去重。

  然后这样做:

  

  

  跟单调队列差不多,两边判断,删减。

  

  注意最后还要判断一下,去尾。像这样:

  

  

  

  

  这题就是这样了。

放代码(调试很多,不删了)

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define Maxn 1100 struct P {double x,y;};
struct L {P a,b;double slop;}l[Maxn];
//半平面只方向向量a->b的左部分
//slop 极角
int cnt; P operator - (P x,P y)
{
P tt;
tt.x=x.x-y.x;
tt.y=x.y-y.y;
return tt;
} P operator + (P x,P y)
{
P tt;
tt.x=x.x+y.x;
tt.y=x.y+y.y;
return tt;
} double Dot(P x,P y) {return x.x*y.x+x.y*y.y;}
double Cross(P x,P y) {return x.x*y.y-x.y*y.x;}
// bool operator < (L x,L y) {return x.slop<y.slop;} bool operator < (L a,L b)
{
if(a.slop!=b.slop)return a.slop<b.slop;
return Cross(a.b-a.a,b.b-a.a)>;
} P operator * (P X,double y)
{
P tt;
tt.x=X.x*y;
tt.y=X.y*y;
return tt;
} P a[Maxn];
L q[Maxn];
int tot; P inter(L a,L b)
{
P X=a.a-a.b,Y=b.a-b.b,nw;
double tt;
nw=b.a-a.a;
tt=Cross(nw,X)/Cross(X,Y);
P ans=b.a+Y*tt;
return ans;
} bool jud(L a,L b,L c)
{
P p=inter(a,b);
return Cross(c.b-c.a,p-c.a)<;
} void opp()
{
for(int i=;i<=cnt;i++)
{
printf("%.2lf %.2lf %.2lf %.2lf = %.2lf \n",l[i].a.x,l[i].a.y,l[i].b.x,l[i].b.y,l[i].slop);
}
printf("\n");
} void output()
{
for(int i=;i<=tot;i++) printf("%2lf %.2lf\n",a[i].x,a[i].y);
printf("\n");
} void op(int L,int R)
{
for(int i=L;i<=R;i++)
printf("%lf %lf %lf %lf\n",l[i].a.x,l[i].a.y,l[i].b.x,l[i].b.y);
printf("\n");
} void ffind()
{
for(int i=;i<=cnt;i++)
l[i].slop=atan2(l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);
sort(l+,l++cnt); // opp(); int L=,R=;
//去重?
tot=;
for(int i=;i<=cnt;i++)
{
if(l[i].slop!=l[i-].slop) tot++;
l[tot]=l[i];
}
cnt=tot;tot=;
// opp();
q[++R]=l[];q[++R]=l[];
for(int i=;i<=cnt;i++)
{
while(L<R&&jud(q[R-],q[R],l[i])) R--;
while(L<R&&jud(q[L+],q[L],l[i])) L++;
q[++R]=l[i];
// op(L,R);
}
while(L<R&&jud(q[R-],q[R],q[L])) R--;
while(L<R&&jud(q[L+],q[L],q[R])) L++;
q[R+]=q[L];
for(int i=L;i<=R;i++)
a[++tot]=inter(q[i],q[i+]);
// output(); // output();
} void init()
{
int n;
/*scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf\n",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y);
}
cnt=n;*/
scanf("%d",&n);
cnt=;
for(int i=;i<=n;i++)
{
int m;
scanf("%d",&m);
P ft,now;
scanf("%lf%lf",&ft.x,&ft.y);
now=ft;
for(int j=;j<=m;j++)
{
P nw;
scanf("%lf%lf",&nw.x,&nw.y);
l[++cnt].b=nw;
l[cnt].a=now;
now=nw;
}
l[++cnt].a=now;l[cnt].b=ft;
// opp();
} for(int i=;i<=cnt;i++)
l[i].slop=atan2(l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);
// opp(); } void get_area()
{
double ans=;
for(int i=;i<tot;i++)
{
ans+=Cross(a[i],a[i+]);
}
ans+=Cross(a[tot],a[]);
if(tot<) ans=;
printf("%.3lf\n",ans/);
} int main()
{
init();
ffind();
// output();
get_area();
return ;
}

用向量法求两直线的交点:

本质就是用面积比表示线段比。

P inter(L a,L b)
{
P X=a.a-a.b,Y=b.a-b.b,nw;
double tt;
nw=b.a-a.a;
tt=Cross(nw,X)/Cross(X,Y);
P ans=b.a+Y*tt;
return ans;
}

半平面交核心过程:

q[++R]=l[1];q[++R]=l[2];
for(int i=3;i<=cnt;i++)
{
while(L<R&&jud(q[R-1],q[R],l[i])) R--;
while(L<R&&jud(q[L+1],q[L],l[i])) L++;
q[++R]=l[i];
}
if(L<R&&jud(q[R-1],q[R],q[L])) R--;

 

代码的具体实现其实没有分上壳和下壳,是一起做的,每次保存有用的半平面,最后相邻的求交点形成凸包。

最后也不用处理上面去尾的情况了,但是注意加一句if,判断最后加的那个半平面是有效的。

if(L<R&&jud(q[R-1],q[R],q[L])) R--;

【倒是对几何画板越来越熟练了,捂脸= =

2016-12-24 09:48:20

【BZOJ 2618】 2618: [Cqoi2006]凸多边形 (半平面交)的更多相关文章

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

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

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

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

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

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

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

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

  5. 2018.07.04 BZOJ 2618 Cqoi2006凸多边形(半平面交)

    2618: [Cqoi2006]凸多边形 Time Limit: 5 Sec Memory Limit: 128 MB Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n ...

  6. bzoj 2618 半平面交模板+学习笔记

    题目大意 给你n个凸多边形,求多边形的交的面积 分析 题意\(=\)给你一堆边,让你求半平面交的面积 做法 半平面交模板 1.定义半平面为向量的左侧 2.将所有向量的起点放到一个中心,以中心参照进行逆 ...

  7. bzoj 2618【半平面交模板】

    #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...

  8. 【BZOJ2618】[CQOI2006]凸多边形(半平面交)

    [BZOJ2618][CQOI2006]凸多边形(半平面交) 题面 BZOJ 洛谷 题解 这个东西就是要求凸多边形的边所形成的半平面交. 那么就是一个半平面交模板题了. 这里写的是平方的做法. #in ...

  9. [CQOI2006]凸多边形(半平面交)

    很明显是一道半平面交的题. 先说一下半平面交的步骤: 1.用点向法(点+向量)表示直线 2.极角排序,若极角相同,按相对位置排序. 3.去重,极角相同的保留更优的 4.枚举边维护双端队列 5.求答案 ...

随机推荐

  1. Cocos移植到Android-Android.mk编译文件

    我们在上一篇博客中年使用的cocos工具对于C和C++源代码进行编译.事实上cocos工具读取<游戏工程目录>\proj.android\jni\目录中的Android.mk文件,进行交叉 ...

  2. sql语句聚合等疑难问题收集

    ------------------------------------------------------------------------------------ 除法运算 select 500 ...

  3. WIN32一些文件及窗口操作

    0,获取指定的系统路径: #include <shlobj.h>     #pragma comment(lib, "shell32.lib") TCHAR szPat ...

  4. Shell 循环读取文件

    使用Shell将Windows环境下的文件拷贝到Linux下面的用法. 在linux下,将dos文件格式转换成linux文件格式的用法,vi打开,然后转到命令格式,执行,然后保存,就可以转换成linu ...

  5. excl剔除不合格数据求平均值

    excl剔除不合格数据求平均值 trimmean函数 正态分布: CONFIDENCE.NORM 函数

  6. 使用c#检测文件正在被那个进程占用

    要检测文件被那个进程占用,需要使用微软提供的工具Handle.exe,这里有微软提供的下载 我们可以在c#中调用Handle.exe 来检测到底哪个进程占用了文件 string fileName = ...

  7. Sublime Text 3 使用备注

    去年开始为了正规化自己的日常编辑工作,在dw,editplus,notap++,st里做了个选择,最终决定改曾经的dw为st. 毕竟dw是上个世纪的东西了,体积比较臃肿了.所以,在这里记录关于st的使 ...

  8. 淘宝IP地址库采集器c#代码

    这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下. 最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息.网上的Ip纯真数据库,下载下来一看 ...

  9. 源码编译安装LNMP环境及配置基于域名访问的多虚拟主机

    实验环境及软件版本: CentOS版本: 6.6(2.6.32.-504.el6.x86_64) nginx版本: nginx-1.6.2 mysql版本:  Mysql-5.6.23 php版本: ...

  10. leetcode学习笔记--开篇

    1 LeetCode是什么? LeetCode是一个在线的编程测试平台,国内也有类似的Online Judge平台.程序开发人员可以通过在线刷题,提高对于算法和数据结构的理解能力,夯实自己的编程基础. ...