LA 3890 半平面交

二分查询答案,判断每一个新形成的向量合在一块能否形成半平面交

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 using namespace std;
  7 #define N 110
  8 #define eps 1e-7
  9
 10 int dcmp(double x)
 11 {
 12     if(fabs(x)<eps) return 0;
 13     else return x<0?-1:1;
 14 }
 15
 16 struct Point{
 17     double x , y;
 18     Point(double x=0 , double y=0):x(x),y(y){}
 19 }po[N] , poly[N];
 20
 21 typedef Point Vector;
 22 Vector vec[N]; //记录每条边上对应的法向量
 23
 24 Vector operator+(Vector a , Vector b) { return Vector(a.x+b.x , a.y+b.y); }
 25 Vector operator-(Point a , Point b) { return Vector(a.x-b.x , a.y-b.y); }
 26 Vector operator*(Vector a , double b) { return Vector(a.x*b , a.y*b); }
 27 Vector operator/(Vector a , double b) { return Vector(a.x/b , a.y/b); }
 28 bool operator==(const Point &a , const Point &b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
 29
 30 double Dot(Vector a  , Vector b) { return a.x*b.x+a.y*b.y; }
 31 double Cross(Vector a , Vector b) { return a.x*b.y - b.x*a.y; }
 32 double Length(Vector a) { return sqrt(Dot(a,a)); }
 33 double Angle(Vector A , Vector B) { return acos(Dot(A,B)) / Length(A) / Length(B); }
 34 double Area2(Point A , Point B , Point C) { return Cross(B-A , C-A); }
 35
 36 Vector Rotate(Vector A , double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad) , A.x*sin(rad)+A.y*cos(rad)); }
 37
 38 Vector Normal(Vector a)
 39 {
 40     double l = Length(a);
 41     return Vector(-a.y/l , a.x/l);
 42 }
 43
 44 struct Line{
 45     Point P;
 46     Vector v;
 47     double ang;
 48     Line(){}
 49     Line(Point P , Vector v):P(P),v(v){ang = atan2(v.y,v.x);}
 50     bool operator<(const Line &m)const {
 51         return ang<m.ang;
 52     }
 53 }line[N] , L[N];
 54
 55 bool OnLeft(Line L , Point P)
 56 {
 57     return Cross(L.v , P-L.P) > 0;
 58 }
 59
 60 Point GetIntersection(Line a , Line b)
 61 {
 62     Vector u = a.P-b.P;
 63     double t = Cross(b.v , u) / Cross(a.v , b.v);
 64     return a.P+a.v*t;
 65 }
 66
 67 /***半平面交的主过程,返回形成半平面交点的个数,无法形成就返回0***/
 68 int HalfplaneIntersection(Line *L , int n , Point *poly)
 69 {
 70     sort(L , L+n);
 71     int first , last;
 72     Point *p = new Point[n];
 73     Line *q = new Line[n];
 74     q[first=last=0] = L[0];
 75     for(int i=1 ; i<n ; i++)
 76     {
 77         while(first<last && !OnLeft(L[i] , p[last-1])) last--;
 78         while(first<last && !OnLeft(L[i] , p[first])) first++;
 79         q[++last] = L[i];
 80         if(fabs(Cross(q[last].v , q[last-1].v)) < eps)
 81         {
 82             last--;
 83             if(OnLeft(q[last] , L[i].P)) q[last]=L[i];
 84         }
 85         if(first<last) p[last-1] = GetIntersection(q[last-1] , q[last]);
 86     }
 87     while(first < last && !OnLeft(q[first] , p[last-1])) last--;
 88     //删除无用平面
 89     if(last-first<=1) return 0;
 90     p[last] = GetIntersection(q[last] , q[first]);
 91
 92     //从deque复制到输出中
 93     int m=0;
 94     for(int i=first ; i<=last ; i++) poly[m++] = p[i];
 95     return m;
 96 }
 97
 98 int main()
 99 {
100    // freopen("a.in" , "r" , stdin);
101     int n;
102     while(scanf("%d" , &n) , n)
103     {
104         for(int i=0 ; i<n ; i++)
105             scanf("%lf%lf" , &po[i].x , &po[i].y);
106
107         for(int i=0 ; i<n ; i++) vec[i] = Normal(po[(i+1)%n]-po[i]);
108         double l=0 , r=20000;
109         while(r-l>eps){
110             double m=(l+r)/2;
111             for(int i=0 ; i<n ; i++) L[i] = Line(po[i]+vec[i]*m , po[(i+1)%n]-po[i]);
112             if(HalfplaneIntersection(L , n , poly)>0) l=m;
113             else r=m;
114         }
115         printf("%.6f\n" , l);
116     }
117     return 0;
118 }
时间: 2024-08-11 01:36:10

LA 3890 半平面交的相关文章

计算几何 半平面交

LA 4992 && hdu 3761 Jungle Outpost 杭电的有点坑啊..一直爆内存,后来发现大白的半平面交模板那里 point *p = new point[n]; line *q = new line[n]这里出了问题,应该是在函数里面申请不了比较大的数组,所以爆内存..我在全局定义了两个数组就不会爆了.. 本来跑了17s多的,后来半平面交sort( l, l + n ) 被我注释了,就跑了9s多,LA上跑了 2s..应该是输入数据比较好,不用按照极角排序..然后就是照着

HDU 2297 半平面交

Run Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 640    Accepted Submission(s): 181 Problem Description Since members of Wuhan University ACM Team are lack of exercise, they plan to particip

关于半平面交

嗯,这是一个很屌的东西.可以把他想象成数学中的线性规划问题,然后自然而然得想到就可以求最优解啦. 如何求解半平面交????? 做法一:暴力枚举点,用该点切割现有的凸多边形,这样的复杂度是O(n^2) 做法二:神奇的分治O(nlogn),然而我并不会.... 做法三:参见2006年朱泽园大神发明的排序增量法:这里就暂时不给详细介绍了. 复杂度O(nlogn),如果把快速排序改成基数排序,复杂度可以进一步降为:O(n)!!!!!! 半平面交的另外一个应用是求多边形的核:可以想象成站在一个多边形的点上

POJ3525 半平面交

题意:求某凸多边形内部离边界最远的点到边界的距离 首先介绍半平面.半平面交的概念: 半平面:对于一条有向直线,它的方向的左手侧就是它所划定的半平面范围.如图所示: 半平面交:多个半平面的交集.有点类似二元函数的线性规划.如图 求半平面交:用的kuangbin模板= = sol:二分答案 二分距离值,按这个值把边界向内缩,再求新的半平面交.如图: 绿色的是原图形,蓝色是按距离值向里面缩进去之后得到的新图形.对这个新图做半平面交即可. 若半平面交存在,说明与边界的距离是该值的点存在(半平面交里面的点

BZOJ 1137: [POI2009]Wsp 岛屿 半平面交

1137: [POI2009]Wsp 岛屿 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 165  Solved: 78[Submit][Status][Discuss] Description Byteotia岛屿是一个凸多边形.城市全都在海岸上.按顺时针编号1到n.任意两个城市之间都有一条笔直的道路相连.道路相交处可以自由穿行.有一些道路被游击队控制了,不能走,但是可以经过这条道路与未被控制的道路的交点.问

bzoj2618[Cqoi2006]凸多边形 半平面交

这是一道半平面交的裸题,第一次写半平面交,就说一说我对半平面交的理解吧. 所谓半平面交,就是求一大堆二元一次不等式的交集,而每个二元一次不等式的解集都可以看成是在一条直线的上方或下方,联系直线的标准方程就可以得出.于是乎这些不等式就可以转化为一些半平面,求的就是半平面交. 而半平面交不可能交出凹多边形(因为凹多边形的定义是有一条边所在的直线能把该多边形分成若干块...YY一下就知道这是不可能的),这是一个十分优美的性质,正类似于凸包(写法也是有些相似的),但半平面交可能交出无界,于是可以加四条类

poj3335 半平面交

题意:给出一多边形.判断多边形是否存在一点,使得多边形边界上的所有点都能看见该点. sol:在纸上随手画画就可以找出规律:按逆时针顺序连接所有点.然后找出这些line的半平面交. 题中给出的点已经按顺时针排好序了,所以只要倒过来一下就可以了.很简单的模板题. 1 #include<vector> 2 #include<list> 3 #include<map> 4 #include<set> 5 #include<deque> 6 #includ

【bzoj2618】[Cqoi2006]凸多边形 半平面交

题目描述 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. 输入 第一行有一个整数n,表示凸多边形的个数,以下依次描述各个多边形.第i个多边形的第一行包含一个整数mi,表示多边形的边数,以下mi行每行两个整数,逆时针给出各个顶点的坐标. 输出 输出文件仅包含一个实数,表示相交部分的面积,保留三位小数. 样例输入 2 6 -2 0 -1 -2 1 -2 2 0 1 2 -1 2 4 0 -3 1 -1 2 2 -1 0 样例输出

POJ 2451 Uyuw&#39;s Concert(半平面交nlgn)

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <vector> #include <