1. 程式人生 > >poj 2398 Toy Storage【二分+叉積】

poj 2398 Toy Storage【二分+叉積】

mes clu print 左右 etc ems eof pac -m

二分點所在區域,叉積判斷左右

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N=1005;
int T,n,m,x1,y1,x2,y2,ans[N],cnt[N];
struct dian
{
    double x,y;
    dian(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    dian operator
+ (const dian &a) const { return dian(x+a.x,y+a.y); } dian operator - (const dian &a) const { return dian(x-a.x,y-a.y); } }; struct bian { dian s,t; bian(dian S=dian(),dian T=dian()) { s=S,t=T; } }q[N]; bool cmp(const bian &a,const
bian &b) { return a.s.x<b.s.x||(a.s.x==b.s.x&&a.t.x<b.t.x); } int read() { int r=0,f=1; char p=getchar(); while(p>‘9‘||p<‘0‘) { if(p==‘-‘) f=-1; p=getchar(); } while(p>=‘0‘&&p<=‘9‘) { r=r*10+p-48; p=getchar(); } return
r*f; } double cj(dian a,dian b) { return a.x*b.y-a.y*b.x; } int main() { while(scanf("%d",&n)&&n) { memset(ans,0,sizeof(ans)); memset(cnt,0,sizeof(cnt)); m=read(),x1=read(),y1=read(),x2=read(),y2=read(); for(int i=0;i<n;i++) { int u=read(),l=read(); q[i]=bian(dian(u,y1),dian(l,y2)); } q[n]=bian(dian(x2,y1),dian(x2,y2)); sort(q,q+1+n,cmp); for(int i=1;i<=m;i++) { int x=read(),y=read(); dian p=dian(x,y); int l=0,r=n,ans; while(l<=r) { int mid=(l+r)>>1; if(cj(q[mid].s-p,q[mid].t-p)<0) { ans=mid; r=mid-1; } else l=mid+1; } cnt[ans]++; } for(int i=0;i<=n;i++) ans[cnt[i]]++; puts("Box"); for(int i=1;i<=m;i++) if(ans[i]) printf("%d: %d\n",i,ans[i]); } return 0; }

poj 2398 Toy Storage【二分+叉積】