[poj] 2318 TOYS || 判断点在多边形内

原题

给出一个矩形玩具箱和其中隔板的位置,求每个玩具在第几个隔间内(保证没有在线上的玩具)


将玩具按x轴排序,记录当前隔板的编号,每次判断是否需要右移(左移)隔板(因为是有序的,所以移动次数左右不厚超过1),(即判断在该隔板的左或右边,)这样就可以解决了!

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 5050
using namespace std;
int n,m,ans[N];

int Read()
{
    int ans=0,fu=1;
    char j=getchar();
    for (;j<'0' || j>'9';j=getchar()) if (j=='-') fu=-1;
    for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
    return ans*fu;
}

struct point
{
    int x,y;
    point() {}
    point(int _x,int _y) : x(_x),y(_y) {}
    bool operator == (const point &b) const
	{
	    return x==b.x && y==b.y;
	}
    bool operator < (const point &b) const
	{
	    if (x==b.x) return y<b.y;
	    return x<b.x;
	}
    point operator - (const point &b) const
	{
	    return point(b.x-x,b.y-y);
	}
    double operator * (const point &b) const
	{
	    return x*b.y-b.x*y;
	}
    void read()
	{
	    x=Read();
	    y=Read();
	}
}s,t,f[N];
struct edge
{
    point a,b;
    bool operator < (const edge &t) const
	{
	    if (a==t.a) return b<t.b;
	    return a<t.a;
	}
}q[N];

void slove()
{
    int now=0;
    for (int i=1;i<=m;i++)
    {
	while ((q[now+1].b-f[i])*(q[now+1].a-f[i])<0 && now<n) now++;
	while ((q[now].b-f[i])*(q[now].a-f[i])>0 && now) now--;
	ans[now]++;
    }
}

int main()
{
    while (~scanf("%d",&n) && n)
    {
	memset(ans,0,sizeof(ans));
	m=Read();
	s.read();
	t.read();
	q[0].a=s;
	q[0].b.x=s.x;
	q[0].b.y=t.y;
	for (int i=1;i<=n;i++)
	    q[i].a.x=Read(),q[i].a.y=s.y,q[i].b.x=Read(),q[i].b.y=t.y;
	for (int i=1;i<=m;i++)
	    f[i].read();
	sort(q+1,q+n+1);
	sort(f+1,f+m+1);
	slove();
	for (int i=0;i<=n;i++)
	    printf("%d: %d\n",i,ans[i]);
	putchar('\n');
    }
    return 0;
}
posted @ 2018-01-01 16:10  Mrha  阅读(169)  评论(0编辑  收藏  举报