算法复习——半平面交(bzoj2618凸多边形)
讲解:
这里套用wuvin神犇的ppt,附上友情链接:http://blog.leanote.com/wuvin
半平面交:

算法流程:

注意事项:

例题:
Description

则相交部分的面积为5.233。
Input
第一行有一个整数n,表示凸多边形的个数,以下依次描述各个多边形。第i个多边形的第一行包含一个整数mi,表示多边形的边数,以下mi行每行两个整数,逆时针给出各个顶点的坐标。
Output
输出文件仅包含一个实数,表示相交部分的面积,保留三位小数。
Sample Input
6
-2 0
-1 -2
1 -2
2 0
1 2
-1 2
4
0 -3
1 -1
2 2
-1 0
Sample Output
HINT
100%的数据满足:2<=n<=10,3<=mi<=50,每维坐标为[-1000,1000]内的整数
题解:
半平面交裸题
心得:
主要是代码很烦吧···一定要理清点与点之间的关系,多画图帮助理解;
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=;
struct point
{
double x;
double y;
}p[N],a[N];
struct line
{
point a;
point b;
double slope;
}l[N],q[N];
inline double operator * (point a,point b)
{
return a.x*b.y-a.y*b.x;
}
inline point operator - (point a,point b)
{
point t;
t.x=a.x-b.x;
t.y=a.y-b.y;
return t;
}
inline point inter(line a,line b)
{
double k1=(b.b-a.a)*(a.b-a.a);
double k2=(a.b-a.a)*(b.a-a.a);
double t=k1/(k1+k2);
point k;
k.x=b.b.x+(b.a.x-b.b.x)*t;
k.y=b.b.y+(b.a.y-b.b.y)*t;
return k;
}
bool jud(line a,line b,line c)
{
point t=inter(a,b);
return (t-c.a)*(c.b-c.a)>;
}
int n,k,cnt,tot;
double ans;
bool comp(line a,line b)
{
if(a.slope!=b.slope) return a.slope<b.slope;
else return (b.b-a.a)*(a.b-a.a)<;
}
void build()
{
sort(l+,l+cnt+,comp);
/*for(int i=1;i<=cnt;i++)
cout<<l[i].a.x<<' '<<l[i].a.y<<' '<<l[i].b.x<<' '<<l[i].b.y<<endl;*/
for(int i=;i<=cnt;i++)
{
if(l[i].slope!=l[i-].slope)tot++;
l[tot]=l[i];
}
int left=,right=;
q[++right]=l[];
q[++right]=l[];
cnt=tot,tot=;
for(int i=;i<=cnt;i++)
{
while(left<right&&jud(q[right-],q[right],l[i])) right--;
while(left<right&&jud(q[left+],q[left],l[i])) left++;
q[++right]=l[i];
}
while(left<right&&jud(q[right-],q[right],q[left])) right--;
while(left<right&&jud(q[left+],q[left],q[right])) left++;
q[right+]=q[left];
for(int i=left;i<=right;i++)
a[++tot]=inter(q[i],q[i+]); }
void getans()
{
if(tot<) return;
a[tot+]=a[];
for(int i=;i<=tot;i++)
ans+=a[i]*a[i+];
ans=fabs(ans)/;
}
int main()
{
//freopen("a.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&k);
for(int j=;j<=k;j++)
scanf("%lf%lf",&p[j].x,&p[j].y);
p[k+]=p[];
for(int j=;j<=k;j++)
{
l[++cnt].a=p[j];
l[cnt].b=p[j+];
}
}
/*for(int i=1;i<=cnt;i++)
cout<<l[i].a.x<<' '<<l[i].a.y<<' '<<l[i].b.x<<' '<<l[i].b.y<<endl;*/
for(int i=;i<=cnt;i++)
l[i].slope=atan2(l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);
build();
getans();
printf("%.3lf\n",ans);
return ;
}
算法复习——半平面交(bzoj2618凸多边形)的更多相关文章
- 【BZOJ2618】[CQOI2006]凸多边形(半平面交)
[BZOJ2618][CQOI2006]凸多边形(半平面交) 题面 BZOJ 洛谷 题解 这个东西就是要求凸多边形的边所形成的半平面交. 那么就是一个半平面交模板题了. 这里写的是平方的做法. #in ...
- 【BZOJ-2618】凸多边形 计算几何 + 半平面交 + 增量法 + 三角剖分
2618: [Cqoi2006]凸多边形 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 959 Solved: 489[Submit][Status] ...
- 【BZOJ 2618】 2618: [Cqoi2006]凸多边形 (半平面交)
2618: [Cqoi2006]凸多边形 Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input 第一 ...
- bzoj 2618 2618: [Cqoi2006]凸多边形(半平面交)
2618: [Cqoi2006]凸多边形 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 656 Solved: 340[Submit][Status] ...
- bzoj 2618: [Cqoi2006]凸多边形 [半平面交]
2618: [Cqoi2006]凸多边形 半平面交 注意一开始多边形边界不要太大... #include <iostream> #include <cstdio> #inclu ...
- 2018.07.04 BZOJ 2618 Cqoi2006凸多边形(半平面交)
2618: [Cqoi2006]凸多边形 Time Limit: 5 Sec Memory Limit: 128 MB Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n ...
- BZOJ - 2618 凸多边形 (半平面交)
题意:求n个凸多边形的交面积. 半平面交模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; ty ...
- 洛谷 P4196 [CQOI2006]凸多边形 (半平面交)
题目链接:P4196 [CQOI2006]凸多边形 题意 给定 \(n\) 个凸多边形,求它们相交的面积. 思路 半平面交 半平面交的模板题. 代码 #include <bits/stdc++. ...
- [CQOI2006]凸多边形(半平面交)
很明显是一道半平面交的题. 先说一下半平面交的步骤: 1.用点向法(点+向量)表示直线 2.极角排序,若极角相同,按相对位置排序. 3.去重,极角相同的保留更优的 4.枚举边维护双端队列 5.求答案 ...
随机推荐
- LeetCode 3Sum Closest 最近似的3sum(2sum方法)
题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...
- Mybatis-Generator逆向生成Po,Mapper,XMLMAPPER(idea)
前文有一篇手工生成的说明,地址: http://www.cnblogs.com/xiaolive/p/4874605.html, 现在这个补充一下在idea里面的自动版本的数据库逆向生成工具: 一.g ...
- 集成iAd广告
在iPhone程序中集成广告,管他能不能赚钱,不放上一个iAd就心有不甘. 参考了下面这篇文章: http://bees4honey.com/blog/tutorial/how-to-add-iad- ...
- 【Selenium-WebDriver问题点】driver和浏览器版本之间的兼容性问题
今天把手头有的一些关于selenium测试的资源整理了一下,分享出来. 1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是 ...
- js 监听页面url锚点变化 window.onpopstate
window.onpopstate = function (event) { if (location.href.indexOf('#') == -1) { location.reload(); } ...
- 漫谈 Clustering (4): Spectral Clustering<转载>
转自http://blog.pluskid.org/?p=287 如果说 K-means 和 GMM 这些聚类的方法是古代流行的算法的话,那么这次要讲的 Spectral Clustering 就可以 ...
- 用python编写九九乘法表
for i in range(1,10): for j in range(1,10): if j >i: print(end='') else: print(j,'*',i,'=',i*j,en ...
- Mac 安装和卸载 Mysql5.7.11 的方法
安装 去http://www.mysql.com/downloads/, 选择最下方的MySQL Community Edition,点击MySQL Community Server的download ...
- 两个input标签之间间隙问题的解决
<input type="text"> <input type="button" value="搜索"> 代码显示效 ...
- sscanf函数详解
#if 0 ,sscanf():从一个字符串中读进与指定格式相符的数据. ,sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源. ,关于正则表 ...