POJ1912 给定n个点 和若干条直线,判断对于一条直线,是否存在两个点在直线的两侧。

显然原命题等价于 凸包与直线是否相交。

O(n)的算法是显而易见的 但是直线数量太多 就会复杂到O(n^2)由于n<=100000 会TLE

凸包有个很好的性质,我们没有利用, 那就是凸包的边相对于要判断的直线是极角有序的!

于是得到算法:

构造好凸包后,二分找凸包上第一个与正向直线夹角大于0的线段和第一个与反向直线夹角大于0的线段

然后判断两线段的起点是否在直线两侧即可。

代码实现有一点注意的细节:不要用上下界的方法实现二分,会很困难,用另外一种调整跳转距离的方法来实现就会简单很多。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const double eps=1e-9; int cmp(double x)
{
if(fabs(x)<eps)return 0;
if(x>0)return 1;
else return -1;
} const double pi=acos(-1.0); inline double sqr(double x)
{
return x*x;
} struct point
{
double x,y;
point (){}
point (double a,double b):x(a),y(b){}
bool input()
{
return scanf("%lf%lf",&x,&y)!=EOF;
}
friend point operator +(const point &a,const point &b)
{
return point(a.x+b.x,a.y+b.y);
}
friend point operator -(const point &a,const point &b)
{
return point(a.x-b.x,a.y-b.y);
}
friend bool operator ==(const point &a,const point &b)
{
return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
}
friend point operator *(const point &a,const double &b)
{
return point(a.x*b,a.y*b);
}
friend point operator*(const double &a,const point &b)
{
return point(a*b.x,a*b.y);
}
friend point operator /(const point &a,const double &b)
{
return point(a.x/b,a.y/b);
}
double norm()
{
return sqr(x)+sqr(y);
}
}; struct line
{
point a,b;
line(){};
line(point x,point y):a(x),b(y)
{ }
};
double det(const point &a,const point &b)
{
return a.x*b.y-a.y*b.x;
} double dot(const point &a,const point &b)
{
return a.x*b.x+a.y*b.y;
} double dist(const point &a,const point &b)
{
return (a-b).norm();
} point rotate_point(const point &p,double A)
{
double tx=p.x,ty=p.y;
return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
} bool parallel(line a,line b)
{
return !cmp(det(a.a-a.b,b.a-b.b));
} bool line_joined(line a,line b,point &res)
{
if(parallel(a,b))return false;
double s1=det(a.a-b.a,b.b-b.a);
double s2=det(a.b-b.a,b.b-b.a);
res=(s1*a.b-s2*a.a)/(s1-s2);
return true;
} bool pointonSegment(point p,point s,point t)
{
return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;
} void PointProjLine(const point p,const point s,const point t,point &cp)
{
double r=dot((t-s),(p-s))/dot(t-s,t-s);
cp=s+r*(t-s);
} struct polygon_convex
{
vector<point>P;
polygon_convex(int Size=0)
{
P.resize(Size);
}
}; struct halfPlane
{
double a,b,c;
halfPlane(point p,point q)
{
a=p.y-q.y;
b=q.x-p.x;
c=det(p,q);
}
halfPlane(double aa,double bb,double cc)
{
a=aa;b=bb;c=cc;
} }; double calc(halfPlane &L,point &a)
{
return a.x*L.a +a.y*L.b+L.c;
} point Intersect(point &a,point &b,halfPlane &L)
{
point res;
double t1=calc(L,a),t2=calc(L,b);
res.x=(t2*a.x-t1*b.x)/(t2-t1);
res.y=(t2*a.y-t1*b.y)/(t2-t1);
//cout<<res.x<<" "<<res.y<<endl;
return res;
} polygon_convex cut(polygon_convex &a,halfPlane &L)
{
int n=a.P.size();
polygon_convex res;
for(int i=0;i<n;i++)
{
if(calc(L,a.P[i])>-eps)res.P.push_back(a.P[i]);
else
{
int j;
j=i-1;
if(j<0)j=n-1;
if(calc(L,a.P[j])>-eps)
res.P.push_back(Intersect(a.P[j],a.P[i],L));
j=i+1;
if(j==n)j=0;
if(calc(L,a.P[j])>-eps)
res.P.push_back(Intersect(a.P[i],a.P[j],L));
}
}
return res;
}
double INF=10000;
polygon_convex core(vector<point> &a)
{
polygon_convex res;
res.P.push_back(point(0,0));
res.P.push_back(point(INF,0));
res.P.push_back(point(INF,INF));
res.P.push_back(point(0,INF));
int n=a.size();
for(int i=0;i<n-1;i+=2)
{
halfPlane L(a[i],a[(i+1)]);
res=cut(res,L);
}
return res;
}
bool comp_less(const point &a,const point &b)
{
return cmp(a.x-b.x)<0||cmp(a.x-b.x)==0&&cmp(a.y-b.y)<0; } polygon_convex convex_hull(vector<point> a)
{
polygon_convex res(2*a.size()+5);
sort(a.begin(),a.end(),comp_less);
a.erase(unique(a.begin(),a.end()),a.end());//删去重复点
int m=0;
for(int i=0;i<a.size();i++)
{
while(m>1&&cmp(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)--m;
res.P[m++]=a[i];
}
int k=m;
for(int i=int(a.size())-2;i>=0;--i)
{
while(m>k&&cmp(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)--m;
res.P[m++]=a[i];
}
res.P.resize(m);
if(a.size()>1)res.P.resize(m-1);
return res;
} bool is_convex(vector<point> &a)
{
for(int i=0;i<a.size();i++)
{
int i1=(i+1)%int(a.size());
int i2=(i+2)%int(a.size());
int i3=(i+3)%int(a.size());
if((cmp(det(a[i1]-a[i],a[i2]-a[i1]))*cmp(det(a[i2]-a[i1],a[i3]-a[i2])))<0)
return false;
}
return true;
}
int containO(const polygon_convex &a,const point &b)
{
int n=a.P.size();
point g=(a.P[0]+a.P[n/3]+a.P[2*n/3])/3.0;
int l=0,r=n;
while(l+1<r)
{
int mid=(l+r)/2;
if(cmp(det(a.P[l]-g,a.P[mid]-g))>0)
{
if(cmp(det(a.P[l]-g,b-g))>=0&&cmp(det(a.P[mid]-g,b-g))<0)r=mid;
else l=mid;
}else
{
if(cmp(det(a.P[l]-g,b-g))<0&&cmp(det(a.P[mid]-g,b-g))>=0)l=mid;
else r=mid;
}
}
r%=n;
int z=cmp(det(a.P[r]-b,a.P[l]-b))-1;
if(z==-2)return 1;
return z;
}
long long int distant(point a,point b)
{
return (int(b.x)-int(a.x))*(int(b.x)-int(a.x))+(int(b.y)-int(a.y))*(int(b.y)-int(a.y));
}
double convex_diameter(polygon_convex &a,int &First,int &Second)
{
vector<point> &p=a.P;
int n=p.size();
double maxd=0;
if(n==1)
{
First=Second=0;
return maxd;
}
#define next(i)((i+1)%n)
for(int i=0,j=1;i<n;++i)
{
while(cmp(det(p[next(i)]-p[i],p[j]-p[i])-det(p[next(i)]-p[i],p[next(j)]-p[i]))<0)
j=next(j);
double d=dist(p[i],p[j]);
if(d>maxd)
{
maxd=d;
First=i,Second=j;
}
d=dist(p[next(i)],p[next(j)]);
if(d>maxd)
{
maxd=d;
First=next(i),Second=next(j);
} }
return maxd;
} double area(vector<point>a)
{
double sum=0;
for(int i=0;i<a.size();i++)
sum+=det(a[(i+1)%(a.size())],a[i]);
return sum/2.;
}
int sumn;
int nex(int a,int b)
{
a=a+b;
while(a<0)a+=sumn;
while(a>=sumn)a-=sumn;
return a;
}
bool Convex_cross_Segment(point a,point b,polygon_convex &pc)
{
sumn=pc.P.size();
if(pc.P.size()<2)return true;
if(pc.P.size()==2)
{
if(cmp(det(a-b,pc.P[0]-a)*det(a-b,pc.P[1]-a))<0)return false;
else return true;
}
int len=pc.P.size()/2,loc1=-1,loc2=-1,locn=pc.P.size()/2;
while(true)
{
if(cmp(det(a-b,pc.P[nex(locn,1)]-pc.P[locn]))>0&&cmp(det(a-b,pc.P[locn]-pc.P[nex(locn,-1)]))<=0)
{loc1=locn;break;}
if(cmp(det(a-b,pc.P[nex(locn,1)]-pc.P[locn]))>0)
{locn=nex(locn,-len);if(len>1)len/=2;continue;}
else{locn=nex(locn,len);if(len>1)len/=2;continue;}
}
len=pc.P.size()/2;
while(true)
{
if(cmp(det(a-b,pc.P[nex(locn,1)]-pc.P[locn]))<0&&cmp(det(a-b,pc.P[locn]-pc.P[nex(locn,-1)]))>=0)
{loc2=locn;break;}
if(cmp(det(a-b,pc.P[nex(locn,1)]-pc.P[locn]))<0)
{locn=nex(locn,-len);if(len>1)len/=2;continue;}
else{locn=nex(locn,len);if(len>1)len/=2;continue;}
}
if(cmp(det(a-b,pc.P[loc1]-a)*det(a-b,pc.P[loc2]-a))<0)return false;
else return true;
}
vector<point>pp;
int main()
{freopen("t.txt","r",stdin);
int N;
scanf("%d",&N);
pp.resize(N);
for(int i=0;i<N;i++)
pp[i].input();
polygon_convex pc=convex_hull(pp);
point a,b;
while(a.input()&&b.input())
if(Convex_cross_Segment(a,b,pc))printf("GOOD\n");
else printf("BAD\n");
return 0;
}

  

POJ1912 A highway and the seven dwarfs (判断凸包与直线相交 logn)的更多相关文章

  1. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  2. POJ 1912 A highway and the seven dwarfs (凸包)

    [题目链接] http://poj.org/problem?id=1912 [题目大意] 给出一些点,表示一些屋子,这些屋子共同组成了村庄,现在要建一些高速公路 问是否经过了村庄. [题解] 这些屋子 ...

  3. poj 1912 A highway and the seven dwarfs

    A highway and the seven dwarfs Time Limit: 8000MS   Memory Limit: 30000K Total Submissions: 2622   A ...

  4. [poj] 1066 Treasure Hunt || 判断直线相交

    原题 在金字塔内有一个宝藏p(x,y),现在要取出这个宝藏. 在金字塔内有许多墙,为了进入宝藏所在的房间必须把墙炸开,但是炸墙只能炸每个房间墙的中点. 求将宝藏运出城堡所需要的最小炸墙数. 判断点和直 ...

  5. POJ2318TOYS(叉积判断点与直线位置)

    题目链接 题意:一个矩形被分成了n + 1块,然后给出m个点,求每个点会落在哪一块中,输出每块的点的个数 就是判断 点与直线的位置,点在直线的逆时针方向叉积 < 0,点在直线的顺时针方向叉积 & ...

  6. UVa 10256 - The Great Divide 判断凸包相交

    模板敲错了于是WA了好几遍…… 判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No. 训练指南上的分析: 1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交.如果相交( ...

  7. Pick-up sticks(判断两条线段是否相交)

    Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8351 Accepted: 3068 Description Stan has ...

  8. 判断两条直线的位置关系 POJ 1269 Intersecting Lines

    两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...

  9. POJ2318 TOYS(叉积判断点与直线的关系+二分)

    Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a prob ...

随机推荐

  1. linux 命令终端提示符显示-bash-4.2#解决方法

    原地址:http://blog.csdn.net/liulihui1988/article/details/52796395 终端提示符显示的是-bash-4.2# 而不是root@主机名 + 路径的 ...

  2. django-1创建项目创建app设置setting、urls、templates、views等

    1. python -m django --version 查看版本 1.11.4 在需要创建项目的目录下执行: 2. django-admin startproject myblog => 创 ...

  3. Far Relative’s Problem (贪心 计算来的最多客人)

    Description Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n  ...

  4. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  5. maven运行tomcat6出现错误Exception starting filter encodingFilter怎么解决

    严重: Exception starting filter encodingFilterjava.lang.ClassCastException: org.springframework.web.fi ...

  6. oracle spool

    http://peter8015.iteye.com/blog/2082467 关于SPOOL(SPOOL是SQLPLUS的命令,不是SQL语法里面的东西.) 对于SPOOL数据的SQL,最好要自己定 ...

  7. linux 常见名词及命令(五)

    计划任务服务之一次性任务: at <时间> 安排一次性任务 atq 或at -l 查看任务列表 at -c 序号 预览任务与设置环境 atrm 序号 删除任务 安排任务示例: 在23:30 ...

  8. Uva - 12230 Crossing Rivers (数学期望)

    你住在村庄A,每天需要过很多条河到另一个村庄B上班,B在A的右边,所有的河都在A,B之间,幸运的是每条船上都有自由移动的自动船, 因此只要到达河左岸然后等船过来,在右岸下船,上船之后船的速度不变.现在 ...

  9. 快速幂取模模板 && 51nod 1013 3的幂的和

    #include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...

  10. 常用的delphi 第三方控件

    Devexpress VCL 这个基本上覆盖了系统界面及数据库展示的方方面面,是做桌面系统必备的一套控件,目前的版本是2011.2.3, 支持win32 及win64. AutoUpgrader 这个 ...