POJ 2398 Toy Storage(计算几何)

这题跟TOYS很类似,嗯,直接在那个上面改的,主要是几个线段的位置是乱序的,需要sort下,然后最后改成记数输出就可以了。

 

 

//POJ2398 Toy Storage
//叉积+二分+排序

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

#define N 50005
#define ll long long

struct Point{
       ll x,y;
};

struct line{
    Point front,to;
}l[N];

//叉积
ll cross(Point &p1,Point &p2){
    return p1.x*p2.y-p1.y*p2.x;
}

ll lr(line &li,Point &p){
    Point p1,p2;
    p1.x=p.x-li.front.x;
    p1.y=p.y-li.front.y;
    p2.x=li.to.x-li.front.x;
    p2.y=li.to.y-li.front.y;
    return cross(p1,p2);    //若p在ll右边则为1,左边为0
}

ll binarysearch(ll left,ll right,Point &p){
    if(left+1==right) return left;
    ll t=(left+right)/2;
    if(lr(l[t],p)>0){
        return binarysearch(t,right,p);
    }
    else return binarysearch(left,t,p);
}

bool cmp(const line &l1,const line &l2){
    if(l1.front.x!=l2.front.x){
        return l1.front.x<l2.front.x;
    }
    return l1.to.x<l2.to.x;
}

ll cnt[N];
ll num[N];

int main(){
    ll i,j,n,m,x1,x2,y1,y2;
    while(cin>>n&&n){
        cin>>m>>x1>>y1>>x2>>y2;

        l[0].front.x=x1;
        l[0].front.y=y2;
        l[0].to.x=x1;
        l[0].to.y=y1;
        l[n+1].front.x=x2;
        l[n+1].front.y=y2;
        l[n+1].to.x=x2;
        l[n+1].to.y=y1;

        for(i=1;i<=n;i++){
            cin>>x1>>x2;
            l[i].front.x=x2;
            l[i].front.y=y2;
            l[i].to.x=x1;
            l[i].to.y=y1;
        }
        sort(l+1,l+n+1,cmp);
        memset(cnt,0,sizeof(cnt));
        memset(num,0,sizeof(num));
        for(i=0;i<m;i++){
            cin>>x1>>y1;
            Point p;
            p.x=x1;
            p.y=y1;
            ll t=binarysearch(0,n+1,p);
            cnt[t]++;
        }
        for(i=0;i<=n;i++){
            num[cnt[i]]++;
        }
        cout<<"Box"<<endl;
        for(i=1;i<=n;i++){
            if(!num[i]) continue;
            cout<<i<<": "<<num[i]<<endl;
        }
    }
    return 0;
}


 

你可能感兴趣的:(计算几何,叉积)