裸半平面交。。
计算几何这个debug难度太大。。还是要学会静态差错。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1e9
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 10005
#define nm 100498
#define pi 3.141592653
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
struct P{
double x,y;
P(double x=0,double y=0):x(x),y(y){}
P operator-(const P&o){return P(x-o.x,y-o.y);}
double operator*(const P&o){return x*o.y-y*o.x;}
}p[NM];
struct L{P a,b;double k;}l[NM];
int n,tmp[NM],q[NM],qh,qt,m;
bool cmp(int x,int y){
if(fabs(l[x].k-l[y].k)<eps)return (l[y].b-l[x].a)*(l[x].b-l[x].a)>eps;
return l[x].k<l[y].k;
}
void intersect(L x,L y,P&p){
double t1=(x.b-y.a)*(x.a-y.a),t2=(y.b-x.b)*(x.a-x.b);
p.x=(y.a.x*t2+y.b.x*t1)/(t1+t2);
p.y=(y.a.y*t2+y.b.y*t1)/(t1+t2);
}
bool judge(L a,L b,L c){
P p;intersect(b,c,p);
return (a.b-a.a)*(p-a.a)+eps<0;
}
void cut(){
sort(tmp+1,tmp+1+n,cmp);m=1;
inc(i,2,n)if(fabs(l[tmp[i]].k-l[tmp[m]].k)>eps)tmp[++m]=tmp[i];
mem(q);qh=1;qt=0;
inc(k,1,m){
int i=tmp[k];
while(qh<qt&&judge(l[i],l[q[qt-1]],l[q[qt]]))qt--;
while(qh<qt&&judge(l[i],l[q[qh+1]],l[q[qh]]))qh++;
q[++qt]=i;
}
while(qh<qt&&judge(l[q[qt]],l[q[qh+1]],l[q[qh]]))qh++;
while(qh<qt&&judge(l[q[qh]],l[q[qt-1]],l[q[qt]]))qt--;
}
int main(){
//freopen("data.in","r",stdin);
int _=read();
while(_--){
n=read();
dec(i,n,1)p[i].x=read(),p[i].y=read();
p[n+1]=p[1];
inc(i,1,n){
l[i].a=p[i];l[i].b=p[i+1];
l[i].k=atan2(p[i+1].y-p[i].y,p[i+1].x-p[i].x);
tmp[i]=i;
}
cut();
//printf("%d %d %d\n",qh,qt,m);
puts(qt-qh>1?"YES":"NO");
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7500 | Accepted: 3003 |
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
[Submit] [Go Back] [Status] [Discuss]