zoukankan      html  css  js  c++  java
  • [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
    ",i,ans[i]);
    	putchar('
    ');
        }
        return 0;
    }
    
  • 相关阅读:
    4.2 省选模拟赛 流浪者 容斥dp
    4.2 省选模拟赛 旅行路线 广义SAM
    4.2 省选模拟赛 摆棋子 网络流 最大流/上下界最小流
    bzoj 4316 小C的独立集
    CF R630 div2 1332 E Height All the Same
    C++多线程の条件变量
    c++多线程のunique和lazy initation
    c++多线程の死锁与防止死锁
    c++多线程の数据竞争和互斥对象
    c++的多线程和多进程
  • 原文地址:https://www.cnblogs.com/mrha/p/8167757.html
Copyright © 2011-2022 走看看