TOYS POJ - 2318

http://poj.org/problem?id=2318

Use the cross product to determine which side of the line determined by the point line segment is divided into two.

Cross product reference https://www.cnblogs.com/flipped/p/7207560.html

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=5e3+10;

ll up[maxn],down[maxn],x[maxn],y[maxn];
int ans[maxn];
ll x1,x2,y1,y2;
int n,m;

ll getval(ll x1,ll y1,ll x2,ll y2)
{
    return x1*y2-x2*y1;
}

int judge(ll xx,ll yy,int pos)
{
    ll res1,res2;
    res1=getval(xx-down[pos],yy-y1,up[pos]-down[pos],y2-y1);
    res2=getval(xx-down[pos+1],yy-y1,up[pos+1]-down[pos+1],y2-y1);
    if(res1*res2<0) return 0;
    else{
        if(res1<0) return -1;
        else return 1;
    }
}

int main()
{
    int i,l,r,mid,res,p;
    while(scanf("%d",&n)!=EOF){
        if(n==0) break;
        scanf("%d%lld%lld%lld%lld",&m,&x1,&y1,&x2,&y2);
        swap(y1,y2);
        up[0]=x1,down[0]=x1;
        up[n+1]=x2,down[n+1]=x2;
        for(i=1;i<=n;i++){
            scanf("%lld%lld",&up[i],&down[i]);
        }
        for(i=1;i<=m;i++){
            scanf("%lld%lld",&x[i],&y[i]);
        }
        for(i=0;i<=n;i++) ans[i]=0;
        for(i=1;i<=m;i++){
            if(x[i]==x1) ans[0]++;
            else if(x[i]==x2) ans[n]++;
            else{
                l=0,r=n;
                while(l<=r){
                    mid=(l+r)/2;
                    res=judge(x[i],y[i],mid);
                    if(res==0){
                        p=mid;
                        break;
                    }
                    else if(res==-1) r=mid-1;
                    else l=mid+1;
                }
                ans[p]++;
            }
        }
        for(i=0;i<=n;i++){
            printf("%d: %d\n",i,ans[i]);
        }
        printf("\n");
    }
    return 0;
}

/*
1 1 0 2 4 0
3 1
3 1
*/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325478977&siteId=291194637