LA3218 Find the Border
题意
分析
虽然只找外轮廓,但是时间复杂度不比PSLG优秀,所以可以当做联系PSLG的题做。
PSLG框架
- 找出所有交点
- 交点按序连边
- 把边按极角序排序
- 逆时针找圈
然后何以会顺时针找出无限区域的边呢?缘于这一段:
for(int i=0;i<d;++i)
prev[G[u][(i+1)%d]]=G[u][i];
边prev连向极角序次小的。极角序最小的边的prev连向极角序最大的。
然后在边界上就会顺时针找下去。但是在内部的时候这些边无疑会被逆时针遍历到,所以没有影响。
于是找出无限区域的轮廓的特殊性在于它的有向面积是负的。
时间复杂度主要是找轮廓时遍历边的\(O(n^2)\)
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<ctime>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
rg T data=0;
rg int w=1;
rg char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
{
data=data*10+ch-'0';
ch=getchar();
}
return data*w;
}
template<class T>T read(T&x)
{
return x=read<T>();
}
using namespace std;
typedef long long ll;
co double eps=1e-8;
int dcmp(double x)
{
return fabs(x)<eps?0:(x<0?-1:1);
}
struct Point
{
double x,y;
Point(double x=0,double y=0)
:x(x),y(y){}
bool operator<(co Point&p)co
{
return dcmp(x-p.x)<0||(dcmp(x-p.x)==0&&dcmp(y-p.y)<0);
}
bool operator==(co Point&p)co
{
return dcmp(x-p.x==0)&&dcmp(y-p.y)==0;
}
};
typedef Point Vector;
Vector operator+(co Vector&A,co Vector&B)
{
return Vector(A.x+B.x,A.y+B.y);
}
Vector operator-(co Vector&A,co Vector&B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator*(co Vector&A,double p)
{
return Vector(A.x*p,A.y*p);
}
double Dot(co Vector&A,co Vector&B)
{
return A.x*B.x+A.y*B.y;
}
double Cross(co Vector&A,co Vector&B)
{
return A.x*B.y-A.y*B.x;
}
double Length(co Vector&A)
{
return sqrt(Dot(A,A));
}
typedef vector<Point> Polygon;
Point LineLineIntersection(co Point&P,co Vector&v,co Point&Q,co Vector&w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
bool SegmentProperIntersection(co Point&a1,co Point&a2,co Point&b1,co Point&b2)
{
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
bool OnSegment(co Point&p,co Point&a1,co Point&a2)
{
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
double PolygonArea(co Polygon&poly)
{
double area=0;
int n=poly.size();
for(int i=1;i<n-1;++i)
area+=Cross(poly[i]-poly[0],poly[(i+1)%n]-poly[0]);
return area/2;
}
struct Edge
{
int from,to;
double ang;
};
co int MAXN=1e4;
struct PSLG
{
int n,m,face_cnt;
double x[MAXN],y[MAXN];
vector<Edge>edges;
vector<int>G[MAXN];
int vis[MAXN*2];
int left[MAXN*2];
int prev[MAXN*2];
vector<Polygon>faces;
double area[MAXN];
void Init(int n)
{
this->n=n;
for(int i=0;i<n;++i)
G[i].clear();
edges.clear();
faces.clear();
}
double Angle(int from,int to)
{
return atan2(y[to]-y[from],x[to]-x[from]);
}
void AddEdge(int from,int to)
{
edges.push_back((Edge){from,to,Angle(from,to)});
edges.push_back((Edge){to,from,Angle(to,from)});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
void Build()
{
for(int u=0;u<n;++u)
{
int d=G[u].size();
for(int i=0;i<d;++i)
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];
}
fill(vis,vis+m,0);
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(Point(x[from],y[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]=PolygonArea(faces[i]);
}
}
}g;
co int MAXP=100;
int n,c;
Point P[MAXP];
Point V[MAXP*(MAXP-1)/2+MAXP];
int ID(Point P)
{
return lower_bound(V,V+c,P)-V;
}
Polygon Simplify(co Polygon&poly)
{
Polygon ans;
int n=poly.size();
for(int i=0;i<n;++i)
{
co Point&a=poly[i];
co Point&b=poly[(i+1)%n];
co Point&c=poly[(i+2)%n];
if(dcmp(Cross(a-b,c-b))!=0)
ans.push_back(b);
}
return ans;
}
void BuildGraph()
{
c=n;
for(int i=0;i<n;++i)
V[i]=P[i];
vector<double>dist[MAXP];
for(int i=0;i<n;++i)
for(int j=i+1;j<n;++j)
if(SegmentProperIntersection(P[i],P[(i+1)%n],P[j],P[(j+1)%n]))
{
Point p=LineLineIntersection(P[i],P[(i+1)%n]-P[i],P[j],P[(j+1)%n]-P[j]);
V[c++]=p;
dist[i].push_back(Length(p-P[i]));
dist[j].push_back(Length(p-P[j]));
}
sort(V,V+c);
c=unique(V,V+c)-V;
g.Init(c);
for(int i=0;i<c;++i)
{
g.x[i]=V[i].x;
g.y[i]=V[i].y;
}
for(int i=0;i<n;++i)
{
Vector v=P[(i+1)%n]-P[i];
double len=Length(v);
dist[i].push_back(0);
dist[i].push_back(len);
sort(dist[i].begin(),dist[i].end());
int sz=dist[i].size();
for(int j=1;j<sz;++j)
{
Point a=P[i]+v*(dist[i][j-1]/len);
Point b=P[i]+v*(dist[i][j]/len);
if(a==b)
continue;
g.AddEdge(ID(a),ID(b));
}
}
g.Build();
Polygon poly;
for(int i=0;i<g.faces.size();++i)
if(g.area[i]<0)
{
poly=g.faces[i];
reverse(poly.begin(),poly.end());
poly=Simplify(poly);
break;
}
int m=poly.size();
printf("%d\n",m);
int start=0;
for(int i=0;i<m;++i)
if(poly[i]<poly[start])
start=i;
for(int i=start;i<m;++i)
printf("%.4lf %.4lf\n",poly[i].x,poly[i].y);
for(int i=0;i<start;++i)
printf("%.4lf %.4lf\n",poly[i].x,poly[i].y);
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;++i)
{
int x,y;
read(x),read(y);
P[i]=Point(x,y);
}
BuildGraph();
}
return 0;
}
LA3218 Find the Border的更多相关文章
- 理解CSS边框border
前面的话 边框是CSS盒模型属性中默默无闻的一个普通属性,CSS3的到来,但得边框属性重新焕发了光彩.本文将详细介绍CSS边框 基础样式 边框是一条以空格分隔的集合样式,包括边框粗细(边框宽度 ...
- css样式之border
border用法详解: 1.border-width 属性设置边框的宽度 可能的值:像素 2.border-style 属性设置边框的样式 可能的值:solid(直线),dashed(虚线),dott ...
- 通过CSS的border绘制三角形
通过css的border 可以绘制出三角形, 不同的样式组合,有着不同的效果,可以控制它的大小,颜色,方向.看下面各种图形,相信可能还有很多图形,大家都没见过. 先写出公共的样式: .border { ...
- css3学习--border
http://blog.sina.com.cn/s/blog_61671b520101gelr.html border-radius border-radius: 50px 20px;上下都是50px ...
- border:none 和border:0区别差异
border:none与border:0的区别体现为两点:一是理论上的性能差异,二是浏览器兼容性的差异. 性能差异: [border:0;]把border设为“0”像素效果等于border-width ...
- WPF 通过Border来画边框
WPF有自己的表格控件DataGrid.ListBox等,如果只是简单的需求,可以通过Border控件来画边框. 比如我们需要给上面的控件加上边框. <Window x:Class=" ...
- css border属性做小三角标
<!doctype html><html> <head> <title></title> <meta charset="ut ...
- 使用border做三角形
网站上经常会使用一些三角形,除了图片的方式,实际上利用border我们可以做出纯CSS的三角形.我们知道border是个边抖可以单独设置,当四个边相交的时候他们是什么时候改变的? .t0{ margi ...
- iPhone 6/plus iOS Safari fieldset border 边框消失
问题:iPhone6 plus 手机浏览网页,fieldset border 边框消失. 示例代码: <div> <fieldset style="border: 1px ...
随机推荐
- AI理论学习笔记(一):深度学习的前世今生
AI理论学习笔记(一):深度学习的前世今生 大家还记得以深度学习技术为基础的电脑程序AlphaGo吗?这是人类历史中在某种意义的第一次机器打败人类的例子,其最大的魅力就是深度学习(Deep Learn ...
- Sharding-Jdbc实现分表分库
Sharding-Jdbc分表分库LogicTable数据分片的逻辑表,对于水平拆分的数据库(表),同一类表的总称.订单信息表拆分为2张表,分别是t_order_0.t_order_1,他们的逻辑表名 ...
- linux tzselect 设置时区
date -R 检查时间 tzselect 按照提示逐步设置 //这里演示的是设置东八区 TZ='Asia/Shanghai'; export TZ 添加到/etc/profile source pr ...
- FZU 1759 Super A^B mod C 指数循环节
Problem 1759 Super A^B mod C Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description G ...
- java 实现Serv-U FTP 和 SFTP 上传 下载
两种ftp使用java的实现方式 ,代码都已测试 第一种:Serv-U FTP 先决条件: 1.Serv-U FTP服务器搭建成功. 2.jar包需要:版本不限制 <!--ftp上传需要的jar ...
- RENOUNCEMENT
I must not think of thee;and,tired yet syrong,I shun the thought that lurks in all delight--The thou ...
- Pandas分组(GroupBy)
任何分组(groupby)操作都涉及原始对象的以下操作之一.它们是 - 分割对象 应用一个函数 结合的结果 在许多情况下,我们将数据分成多个集合,并在每个子集上应用一些函数.在应用函数中,可以执行以下 ...
- java socket - 半关闭
通常,使用关闭输出流来表示输出已经结束.但在进行网络通信时则不能这样做.因为我们关闭输出流时,该输出流对应的Socket也将随之关闭,这样程序将无法再从该socket中读取数据. 为了应付这种情况,s ...
- D3.js学习笔记(三)——创建基于数据的SVG元素
目标 在这一章,你将会使用D3.js,基于我们的数据来把SVG元素添加到网页中.这一过程包括:把数据绑定到元素上,然后在使用这些元素来可视化我们的数据. 注意:不同于前几章,我们从一个完整的代码开始, ...
- Asp.net使用powershell管理hyper-v
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...