题意

PDF

分析

虽然只找外轮廓,但是时间复杂度不比PSLG优秀,所以可以当做联系PSLG的题做。

PSLG框架

  1. 找出所有交点
  2. 交点按序连边
  3. 把边按极角序排序
  4. 逆时针找圈

然后何以会顺时针找出无限区域的边呢?缘于这一段:

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的更多相关文章

  1. 理解CSS边框border

    前面的话   边框是CSS盒模型属性中默默无闻的一个普通属性,CSS3的到来,但得边框属性重新焕发了光彩.本文将详细介绍CSS边框 基础样式   边框是一条以空格分隔的集合样式,包括边框粗细(边框宽度 ...

  2. css样式之border

    border用法详解: 1.border-width 属性设置边框的宽度 可能的值:像素 2.border-style 属性设置边框的样式 可能的值:solid(直线),dashed(虚线),dott ...

  3. 通过CSS的border绘制三角形

    通过css的border 可以绘制出三角形, 不同的样式组合,有着不同的效果,可以控制它的大小,颜色,方向.看下面各种图形,相信可能还有很多图形,大家都没见过. 先写出公共的样式: .border { ...

  4. css3学习--border

    http://blog.sina.com.cn/s/blog_61671b520101gelr.html border-radius border-radius: 50px 20px;上下都是50px ...

  5. border:none 和border:0区别差异

    border:none与border:0的区别体现为两点:一是理论上的性能差异,二是浏览器兼容性的差异. 性能差异: [border:0;]把border设为“0”像素效果等于border-width ...

  6. WPF 通过Border来画边框

    WPF有自己的表格控件DataGrid.ListBox等,如果只是简单的需求,可以通过Border控件来画边框. 比如我们需要给上面的控件加上边框. <Window x:Class=" ...

  7. css border属性做小三角标

    <!doctype html><html> <head> <title></title> <meta charset="ut ...

  8. 使用border做三角形

    网站上经常会使用一些三角形,除了图片的方式,实际上利用border我们可以做出纯CSS的三角形.我们知道border是个边抖可以单独设置,当四个边相交的时候他们是什么时候改变的? .t0{ margi ...

  9. iPhone 6/plus iOS Safari fieldset border 边框消失

    问题:iPhone6 plus 手机浏览网页,fieldset border 边框消失. 示例代码: <div> <fieldset style="border: 1px ...

随机推荐

  1. 2018年Java面试题搜集

    2018年Java面试题搜集 一.Servlet执行流程(浏览器访问servlet的过程容器) 客户端发起http请求,web服务器将请求发送到servlet容器,servlet容器解析url并根据w ...

  2. java中TreeMap集合的常用方法

    实现Map集合的方法这里就不在讲了 https://www.cnblogs.com/xiaostudy/p/9510763.html public Map.Entry<K,V> ceili ...

  3. struts2——上传图片格式

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. segment fault本质

    要谈segment fault,必须要谈指针. 指针的本质是什么?只不过是一种带*的数据类型,其特色有: 1.宽度 2.声明 3.赋值 4.++与-- 5.+与- 6.求差值 7.比较 当声明int ...

  5. ECS vs. Kubernetes 类似而又不同

    C2Container Service (ECS)和Kubernetes (K8s) 都解决了同样的问题:跨越主机集群管理容器.ECS和Kubernetes之间的斗争让我想起了vi和Emacs之间的编 ...

  6. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  7. canvas画的北斗七星和大熊座

    用canvas画的北斗七星和大熊座,主要用到的知识点是:canvas.定时器. html代码: <body> <canvas id="canvas" width= ...

  8. spring4x,暂时停更

    spring4x,暂时停更 鄙人愚笨,没有spring基础,直接上了spring4x,发现无法理解(另外spring4x实战课本演示不详,本人学识有限),现从spring3开始.

  9. 20165332实验二 Java面向对象程序设计

    20165332实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 ...

  10. URL的编码

    + URL 中+号表示空格 %2B 空格 URL中的空格可以用+号或者编码 %20 / 分隔目录和子目录 %2F ? 分隔实际的 URL 和参数 %3F % 指定特殊字符 %25 # 表示书签 %23 ...