【POJ1912】【Ceoi2002】—A highway and the seven dwarfs(凸包)
题意:给出平面上的 个点,对于 条直线,依次判断这 个点是否在每条直线的同一侧。()
考虑对于每一条直线,显然我们要找到凸包上距离这条直线最远的2个点,判一下方向
考虑到类似于旋转卡壳对踵的的思想
记录一下每条边的斜率,然后二分找到的就是最远的点了
#include<bits/stdc++.h>
using namespace std;
inline int read(){
char ch=getchar();
int res=0,f=1;
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch))res=res*10+(ch^48),ch=getchar();
return res*f;
}
const int N=100005;
const double pi=acos(-1);
const double eps=1e-8;
struct point{
double x,y;
point(double a=0,double b=0){
x=a,y=b;
}
friend inline point operator +(const point &a,const point &b){
return point(a.x+b.x,a.y+b.y);
}
friend inline point operator -(const point &a,const point &b){
return point(a.x-b.x,a.y-b.y);
}
friend inline double operator *(const point &a,const point &b){
return (a.x*b.y-a.y*b.x);
}
inline int calc(){
return x*x+y*y;
}
}p[N],q[N],a,b;
int n,top;
double af[N];
inline bool comp(const point &a,const point &b){
double res=(a-p[1])*(b-p[1]);
return (res==0)?((a-p[1]).calc()<(b-p[1]).calc()):(res>0);
}
inline void graham(){
int idx=1;
for(int i=2;i<=n;i++){
if(p[idx].y<p[i].y||(p[idx].y==p[i].y&&p[i].x<p[idx].x))
idx=i;
}
if(idx!=1)swap(p[idx],p[1]);
sort(p+2,p+n+1,comp);
q[++top]=p[1];
for(int i=2;i<=n;++i){
while(top>=3&&((p[i]-q[top-1])*(q[top]-q[top-1])>=0))
top--;
q[++top]=p[i];
}
q[top+1]=q[1];
for(int i=1;i<=top;i++)
af[i]=atan2(q[i+1].y-q[i].y,q[i+1].x-q[i].x);
}
int main(){
n=read();
for(int i=1;i<=n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
if(n>1)graham();
while(scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y)!=EOF){
if(n==1){
cout<<"GOOD"<<'\n';continue;
}
double l1=atan2(b.y-a.y,b.x-a.x),l2=atan2(a.y-b.y,a.x-b.x);
int pos1=lower_bound(af+1,af+top+1,l1)-af;
int pos2=lower_bound(af+1,af+top+1,l2)-af;
point c=q[pos1],d=q[pos2];
if(((b-a)*(c-a))*((b-a)*(d-a))<=0&&(((d-c)*(a-c))*((d-c)*(b-c))))
cout<<"BAD"<<'\n';
else cout<<"GOOD"<<'\n';
}
}