poj 3335 Rotating Scoreboard

本文介绍了一种算法,用于确定是否能在给定的多边形大厅内找到一个位置放置旋转记分牌,确保沿大厅边界的所有观众都能看到。通过半平面交算法实现,特别考虑了输入点的顺时针顺序。
Rotating Scoreboard
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7085 Accepted: 2835

Description

This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the edges of the polygon. We want to place a rotating scoreboard somewhere in the hall such that a spectator sitting anywhere on the boundary of the hall can view the scoreboard (i.e., his line of sight is not blocked by a wall). Note that if the line of sight of a spectator is tangent to the polygon boundary (either in a vertex or in an edge), he can still view the scoreboard. You may view spectator's seats as points along the boundary of the simple polygon, and consider the scoreboard as a point as well. Your program is given the corners of the hall (the vertices of the polygon), and must check if there is a location for the scoreboard (a point inside the polygon) such that the scoreboard can be viewed from any point on the edges of the polygon.

Input

The first number in the input line, T is the number of test cases. Each test case is specified on a single line of input in the form n x1 y1 x2 y2 ... xn yn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xi yi sequence specify the vertices of the polygon sorted in order.

Output

The output contains T lines, each corresponding to an input test case in that order. The output line contains either YES or NO depending on whether the scoreboard can be placed inside the hall conforming to the problem conditions.

Sample Input

2
4 0 0 0 1 1 1 1 0
8 0 0  0 2  1 2  1 1  2 1  2 2  3 2  3 0

Sample Output

YES
NO

Source




【分析】
半平面交...和上一道一模一样。
只不过这次给的点对变成了顺时针的,相应的判断要反一下。
事实上如果题目没告诉是顺还是逆只要把点对正反都跑一遍就好啦。

ps:叉积>0 ,点在向量左侧。否则在右侧


【代码】

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define eps 1e-8
#define ll long long
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=50005;
int n,h,t,ln,T;
int order[mxn],que[mxn];
struct point {double x,y;} p[mxn];
struct line {point a,b;double ang;} l[mxn];
inline int sign(double k)
{
	if(fabs(k)<eps) return 0;
	return k>0?1:-1;
}
inline double cross(point p0,point p1,point p2)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
inline bool comp(int u,int v)
{
	int d=sign(l[u].ang-l[v].ang);
	if(!d) return sign(cross(l[u].a,l[v].a,l[v].b))<0;
	return d<0;
}
inline void get(line l1,line l2,point &p)
{
	double d1,d2;
	d1=cross(l2.a,l1.b,l1.a);
	d2=cross(l1.b,l2.b,l1.a);
	p.x=(l2.a.x*d2+l2.b.x*d1)/(d1+d2);
	p.y=(l2.a.y*d2+l2.b.y*d1)/(d1+d2);
}
inline bool judge(line l0,line l1,line l2)
{
	point p;
	get(l1,l2,p);
	return sign(cross(p,l0.a,l0.b))>0;
}
inline void PHI()
{
	int i,j;
	sort(order,order+ln,comp);
	for(i=1,j=0;i<ln;i++)
	  if(sign(l[order[i]].ang-l[order[j]].ang)>0)
	    order[++j]=order[i];
	ln=j,h=0,t=1;
	que[0]=order[0],que[1]=order[1];
	fo(i,2,ln)
	{
		while(h<t && judge(l[order[i]],l[que[t-1]],l[que[t]])) t--;
		while(h<t && judge(l[order[i]],l[que[h+1]],l[que[h]])) h++;
		que[++t]=order[i];
	}
	while(h<t && judge(l[order[h]],l[que[t-1]],l[que[t]])) t--;
	while(h<t && judge(l[order[t]],l[que[h+1]],l[que[h]])) h++;
}
inline void addline(double x1,double y1,double x2,double y2)
{
	l[ln].a.x=x1,l[ln].a.y=y1;
	l[ln].b.x=x2,l[ln].b.y=y2;
	l[ln].ang=atan2(x2-x1,y2-y1);
	order[ln]=ln,ln++;
}
int main()
{
	int i,j;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		fo(i,0,n-1) scanf("%lf%lf",&p[i].x,&p[i].y);
		for(ln=i=0;i<n-1;i++)
		  addline(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
		addline(p[i].x,p[i].y,p[0].x,p[0].y);
		PHI();
		if(t-h>1) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值