UVALive 7281 Saint John Festival (求凸包+判断点是否在凸包内(O(logn)复杂度))

UVALive 7281 Saint John Festival (求凸包+判断点是否在凸包内(O(logn)复杂度))

题目链接:https://vjudge.net/problem/UVALive-7281

题目大意:给出n个大点,和m个小点,判断有多少个小点在由三个大点组成的三角形内。

思路:题意可以转化为,判断有多少个小点在大点组成的凸包内。

那么就是一个凸包模板和一个判断点是否在凸包内(O(logn)复杂度)的模板

这里解释下判断点是否在凸包内的模板:

就是取其中的一个点,他和其他点可以组成n-2个三角形,利用二分判断差积,判断他是在当前三角形内,还是在三角形左边,还是在三角形右边,或者是三角形外。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    bool operator==(const Point &B)const
    {
        return dcmp(x-B.x)==0 && dcmp(y-B.y)==0;
    }
    bool operator<(const Point &B)const
    {
        return dcmp(x-B.x)<0 || (dcmp(x-B.x)==0 && dcmp(y-B.y)<0);
    }
};
typedef Point Vector;
Vector operator-(Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}
double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
int ConvexHull(Point *p,int n,Point *ch)
{
    sort(p,p+n);
    n=unique(p,p+n)-p;
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}

bool check(Point A,Point*p,int n )//判断点在凸包内模板 O(logn)
{
    int l=1,r=n-2,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        double a1=Cross(p[mid]-p[0],A-p[0]);
        double a2=Cross(p[mid+1]-p[0],A-p[0]);
        if(a1>=0&&a2<=0)
        {
            if(Cross(p[mid+1]-p[mid],A-p[mid])>=0)return true;
            return false;
        }
        else if(a1<0)
        {
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    }
    return false;
}

const int maxn= 50000+500 ;
Point p[maxn],ch[maxn];

int main()
{
	int n,m;
    scanf("%d",&n);
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
		m=ConvexHull(p,n,ch);
        n=m;
		scanf("%d",&m);
		int cnt=0;
        for(int i=0;i<m;++i)
        {
            Point q;
            scanf("%lf%lf",&q.x,&q.y);
            if(check(q,ch,n)) cnt++;
        }
        printf("%d\n",cnt);
    }

    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值