POJ1228 稳定凸包

看题半天没看懂意思,以为就是判断是否有凸包结果。。。。看了题解才知道,是要确定一个凸包是否唯一,即不能通过新增点变成新的凸包,也就是凸包的每条边上至少有3个点。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#define maxl 1010
#define eps 1e-8

using namespace std;

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

struct point
{
	double x,y;
	point(double a=0,double b=0)
	{
		x=a;y=b;
	}
	point operator - (const point &b)const
	{
		return point(x-b.x,y-b.y);
	}
	bool operator == (const point &b)const
	{
		return sgn(x-b.x)==0 && sgn(y-b.y)==0;
	}
	bool operator != (const point &b)const
	{
		return sgn(x-b.x)!=0 || sgn(y-b.y)!=0;
	}
	inline double norm()
	{
		return sqrt(x*x+y*y);
	}
};
inline double dot(const point &a,const point &b)
{
	return a.x*b.x+a.y*b.y;
}
inline double det(const point &a,const point &b)
{
	return a.x*b.y-a.y*b.x;
}

struct polygon_convex
{
	vector<point> P;
	polygon_convex(int size=0)
	{
		P.resize(size);
	}
};

inline bool cmp(const point &a,const point &b)
{
	if(sgn(a.x-b.x)==0)
		return sgn(a.y-b.y)<0;
	return sgn(a.x-b.x)<0;
}
polygon_convex convex_hull(vector<point> a)
{
	polygon_convex res(2*a.size()+5);
	sort(a.begin(),a.end(),cmp);
	a.erase(unique(a.begin(),a.end()),a.end());
	int m=0,l=a.size();
	for(int i=0;i<a.size();i++)
	{
		while(m>1 && sgn(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 && sgn(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;
}

int n;
vector<point> a;
bool ans;

inline void prework()
{
	a.clear();
	scanf("%d",&n);
	point p;
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%lf",&p.x,&p.y);
		a.push_back(p);
	}
}

inline bool point_on_seg(point &a,point &b,point &c)
{
	return sgn(det(a-b,a-c))==0 && sgn(dot(a-b,a-c))<=0;
}

inline void mainwork()
{
	ans=true;
	polygon_convex res=convex_hull(a);
	if(res.P.size()<3)
	{
		ans=false;
		return;
	} 
	res.P.push_back(res.P[0]); 
	bool flag;int l=res.P.size(),l2=a.size();
	for(int i=1;i<l && ans;i++)
	{
		int num=0;
		for(int j=0;j<l2;j++)
		if(point_on_seg(a[j],res.P[i],res.P[i-1]))
			num++;
		if(num<3)
			ans=false;
	} 
}

inline void print()
{
	if(ans)
		puts("YES");
	else
		puts("NO");
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

上面那个由于n=1000,所以直接n方求每条边上多少点了了,其实也可以O(n)求

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#define maxl 1010
#define eps 1e-8

using namespace std;

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

struct point
{
	double x,y;
	point(double a=0,double b=0)
	{
		x=a;y=b;
	}
	point operator - (const point &b)const
	{
		return point(x-b.x,y-b.y);
	}
	bool operator == (const point &b)const
	{
		return sgn(x-b.x)==0 && sgn(y-b.y)==0;
	}
	bool operator != (const point &b)const
	{
		return sgn(x-b.x)!=0 || sgn(y-b.y)!=0;
	}
	inline double norm()
	{
		return sqrt(x*x+y*y);
	}
};
inline double dot(const point &a,const point &b)
{
	return a.x*b.x+a.y*b.y;
}
inline double det(const point &a,const point &b)
{
	return a.x*b.y-a.y*b.x;
}

struct polygon_convex
{
	vector<point> P;
	polygon_convex(int size=0)
	{
		P.resize(size);
	}
};

int n;
int num[maxl];
vector<point> a;
bool ans;

inline bool cmp(const point &a,const point &b)
{
	if(sgn(a.x-b.x)==0)
		return sgn(a.y-b.y)<0;
	return sgn(a.x-b.x)<0;
}

inline bool point_on_seg(point &a,point &b,point &c)
{
	return sgn(det(a-b,a-c))==0 && sgn(dot(a-b,a-c))<=0;
}

polygon_convex convex_hull(vector<point> a)
{
	polygon_convex res(2*a.size()+5);
	sort(a.begin(),a.end(),cmp);
	a.erase(unique(a.begin(),a.end()),a.end());
	int m=0,l=a.size();
	for(int i=0;i<a.size();i++)
	{
		num[m]=1;
		while(m>1 && sgn(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)
		{	
			if(point_on_seg(res.P[m-1],a[i],res.P[m-2]))
				num[m-1]+=num[m],num[m]=0;
			else
				num[m-1]=num[m],num[m]=0;
			--m;
		}
		res.P[m++]=a[i];
	}
	int k=m;
	for(int i=int(a.size())-2;i>=0;i--)
	{
		num[m]=1;
		while(m>k && sgn(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)
		{
			if(point_on_seg(res.P[m-1],a[i],res.P[m-2]))
				num[m-1]+=num[m],num[m]=0;
			else
				num[m-1]=num[m],num[m]=0;
			--m;
		}
		res.P[m++]=a[i];
	}
	res.P.resize(m);
	if(a.size()>1)
		res.P.resize(m-1);
	return res;
}

inline void prework()
{
	a.clear();
	scanf("%d",&n);
	memset(num,0,sizeof(int)*(n+1));
	point p;
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%lf",&p.x,&p.y);
		a.push_back(p);
	}
}

inline void mainwork()
{
	ans=true;
	polygon_convex res=convex_hull(a);
	if(res.P.size()<3)
	{
		ans=false;
		return;
	} 
	res.P.push_back(res.P[0]); 
	bool flag;int l=res.P.size(),l2=a.size();
	/*for(int i=1;i<l && ans;i++)
	{
		int num=0;
		for(int j=0;j<l2;j++)
		if(point_on_seg(a[j],res.P[i],res.P[i-1]))
			num++;
		if(num<3)
			ans=false;
	} */
	for(int i=1;i<l;i++)
	if(num[i]<2)
		ans=false;
}

inline void print()
{
	if(ans)
		puts("YES");
	else
		puts("NO");
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值