UVA 11072 - Points(凸包+点在多边形内判定)

题目:Points

题意:给出两个点集A和B,对于B中每个点,问能否在A中找到不同的三个点组成三角形,使得该点落在这个三角形中。

先对A求凸包,很明显,如果一个点落在A的凸包里,那么肯定能在A中找到一个三角形使得该点在里面,反过来如果不在凸包里,就不可能找到。

点在多边形内的判定有多种方法,这里因为已经先求凸包了,所以可以利用点和线段的位置关系,如果点在所有线段的左边或是线段上(这里点在边上也算在多边形内),则说明点在多边形内。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
inline void in(int &x){
	x=0;
	char c=getchar();
	bool mk=0;
	while(c<48 || c>57){
		if(c=='-')	mk=1;
		c=getchar();
	}
	while(c>=48 && c<=57){
		x = x*10+c-48;
		c = getchar();
	}
	if(mk)	x = -x;
}
struct Point{
	int x, y;
	Point(){}
	Point(int x, int y):x(x),y(y){}
	bool operator < (const Point &tmp)const{
		return x<tmp.x || (x==tmp.x&&y<tmp.y);
	}
	Point operator - (Point A){
		return Point(x-A.x, y-A.y);
	}
};
#define pb push_back
int det(Point A, Point B){
	return A.x*B.y - A.y*B.x;
}
vector<Point> V;
Point ch[100010];
int m;
char ans[2][10]={"inside","outside"};
void ConvexHull(){
	m = 0;
	for(int i=0; i<V.size(); i++){
		while(m>1 && det(ch[m-1]-ch[m-2], V[i]-ch[m-2]) <= 0)	m--;
		ch[m++] = V[i];
	}
	int k = m;
	for(int i=V.size()-2; i>=0; i--){
		while(m>k && det(ch[m-1]-ch[m-2], V[i]-ch[m-2]) <= 0)	m--;
		ch[m++] = V[i];
	}
}
bool find(){
	int x, y;
	in(x); in(y);
	Point P = Point(x,y);
	for(int i=0; i<m-1; i++){
		if(det(ch[i+1]-ch[i], P-ch[i]) < 0)	return 1;
	}
	return 0;
}
int main(){
	int n, x, y;
	while(~scanf("%d", &n)){
		V.clear();
		for(int i=0; i<n; i++){
			in(x); in(y);
			V.pb(Point(x,y));
		}
		sort(V.begin(), V.end());
		ConvexHull();
		in(n);
		while(n--){
			puts(ans[find()]);
		}
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值