POJ 1912 A highway and the seven dwarfs(O(log N)求直线与凸包是否相交)

20 篇文章 0 订阅
4 篇文章 0 订阅
A highway and the seven dwarfs
Time Limit: 8000MS Memory Limit: 30000K
Total Submissions: 2454 Accepted: 485
Case Time Limit: 3000MS

Description

Once upon a time, there was a land where several families of dwarfs were living. This land was called Dwarfland. Each family lived in one house. Dwarfs were often visiting their friends from the other families. Because the Dwarfland was free of evil, it happened that each dwarf visited each other during some short period of time.

Once, the humans living in countries around Dwarfland decided to build several straight highways. As the humans weren't aware of the dwarfs, some of the planned highways passed through Dwarfland. The dwarfs discovered this and were quite unhappy about it. The dwarfs are very little, and also very slow, so they are unable to cross the highway safely.

The dwarfs managed to get the plans for the highways somehow, and now they need your help. They would like to keep on visiting each other, so they don't like those highways which divide their houses into two non-empty parts. After they find out which highways they don't like, they will magically prevent the humans from building them.

The dwarfs are very little, and cannot reach the keyboard. So they asked for your help.

Task

Given is a number N of points (houses) in the plane and several straight lines (highways). For each given line, your task is to determine whether all N points lie on the same side of the line or not. Your program has to output the answer for the currently processed line before reading the description of the next one. You may assume that no highway passes through any of the houses.

Input

Your program is supposed to read the input from the standard input and write its output to the standard output. The first line of the input contains one integer N ( 0 < = N < = 100 000). N lines follow, the i-th of them contains two real numbers xi, yi ( -10 9 < = xi, yi < = 10 9) separated by a single space - the coordinates of the i-th house.

Each of the following lines contains four real numbers X1, Y1, X2, Y2 ( -10 9 < = X1, Y1, X2, Y2 < = 10 9) separated by a single space. These numbers are the coordinates of two different points [X1, Y1] and [X2, Y2], lying on the highway.

Output

For each line of input, your program is supposed to output a line containing the string "GOOD" if all of the given points are on the same side of the given line, or "BAD" if the given line divides the points.

Sample Input

4
0.0 0
6.00 -0.001
3.125 4.747
4.747 0.47
5 3 7 0
4 -4.7 7 4.7
4 47 4 94

Sample Output

GOOD
BAD
BAD

Hint

Huge input,scanf is recommended.

Source


题目大意:

    有N个点,每次输入两点,确定一条直线,问这条直线是否把这些点分成两部分。


解题思路:

    很明显,我们需要先用着N个点构成一个凸包,再判断这条直线是否与凸包相交即可。

    开始用O(N)的暴力方法判断相交竟然TLE了。后来看了大神的题解才知道有O(log N)的判断方法。


AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
#define eps 1e-10

const double PI=acos(-1.0);
const int maxn=100000+3;
int N;
double as[maxn];//保存极角

//考虑误差的加法运算
double add(double a,double b)
{
    if(abs(a+b)<eps*(abs(a)+abs(b)))
        return 0;
    return a+b;
}

int sgn(double x)
{
    if(fabs(x) < eps)return 0; if(x < 0)return -1;
    else return 1;
}

//二维向量结构体
struct P
{
    double x,y;
    P(){}
    P(double x,double y):x(x),y(y){}
    P operator+(const P &p)
    {
        return P(add(x,p.x),add(y,p.y));
    }
    P operator -(const P &b)const
    {
        return P(x - b.x,y - b.y);
    }
    P operator*(double d)
    {
        return P(x*d,y*d);
    }
    //叉积
    double operator ^(const P &b)const 
    {
        return x*b.y - y*b.x; }
    //点积
    double operator *(const P &b)const 
    {
        return x*b.x + y*b.y; }
    //绕原点旋转角度B(弧度值),后x,y的变化 
    void transXY(double B)
    {
        double tx = x,ty = y;
        x = tx*cos(B) - ty*sin(B); y = tx*sin(B) + ty*cos(B);
    }
}ps[maxn];

//字典序比较
bool cmp_x(const P &p,const P &q)
{
    if(p.x!=q.x)
        return p.x<q.x;
    return p.y<q.y;
}

//求凸包
vector<P> convex_hull(P *ps,int n)
{
    sort(ps,ps+n,cmp_x);
    int k=0;//凸包的顶点数
    vector<P> qs(n*2);//构造中的凸包
    //构造凸包下侧
    for(int i=0;i<n;++i)
    {
        while(k>1&&((qs[k-1]-qs[k-2])^(ps[i]-qs[k-1]))<=0)
            --k;
        qs[k++]=ps[i];
    }
    //构造凸包的上侧
    for(int i=n-2,t=k;i>=0;--i)
    {
        while(k>t&&(qs[k-1]-(qs[k-2])^(ps[i]-qs[k-1]))<=0)
            --k;
        qs[k++]=ps[i];
    }
    qs.resize(k-1);
    return qs;
}

inline double normalize(double r)//把极角转换为[-PI/2,3*PI/2)
{
    if (r < -PI / 2.0 + eps) r += PI * 2;
    return r;
}

inline double atan2(const P& p)
{
    return normalize(atan2(p.y, p.x));
}

int main()
{
    scanf("%d",&N);
    for(int i=0;i<N;++i)
        scanf("%lf%lf",&ps[i].x,&ps[i].y);
    vector<P> qs;
    int n=0;
    if(N>1)
    {
        qs=convex_hull(ps,N);
        n=qs.size();
        qs.push_back(qs[0]);
    }
    for(int i=0;i<n;++i)
        as[i]=atan2(qs[i+1]-qs[i]);
    sort(as,as+n);
    P p1,p2;
    while(~scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y))
    {
        if(N<2)//N小于2特判否则会RE
        {
            puts("GOOD");
            continue;
        }
        int i=upper_bound(as,as+n,atan2(p1-p2))-as;//直线两侧各找一点,若两点在直线两侧,则直线一定分割凸包
        int j=upper_bound(as,as+n,atan2(p2-p1))-as;
        puts(((((p2-p1)^(qs[i]-p1))*((p2-p1)^qs[j]-p1))>-eps)?"GOOD":"BAD");
        
    }
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值