题意: 给这个形状,与一些点的坐标,问那个空间里分别有多少个点。
知识点 :计算几何+二分查找
#include<iostream> #include<cstring> using namespace std; struct Point { int x,y; }a; struct Line { Point a,b; }l[5005]; int sum[5005],n; int cross(Point a1,Point a2,Point a0) { int x1=a1.x-a0.x,y1=a1.y-a0.y; int x2=a2.x-a0.x,y2=a2.y-a0.y; return x2*y1-x1*y2; } int s(Point a) { int L=0,R=n,mid; while(L<R) { mid=(L+R)/2; if((cross(a,l[mid].b,l[mid].a)>0&&cross(a,l[mid-1].b,l[mid-1].a)<0)||(cross(a,l[mid].b,l[mid].a)<0&&cross(a,l[mid+1].b,l[mid+1].a)>0))//位置就以mid的为中边 return mid; if(cross(a,l[mid].b,l[mid].a)>0&&cross(a,l[mid-1].b,l[mid-1].a)>0)//位置在mid左边 R=mid-1; else //位置在mid右边 L=mid+1; } } int main() { int m,x1,y1,x2,y2,ans,i; while(scanf("%d",&n)!=EOF) { if(n==0) break; scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2); l[0].a.x=x1,l[0].a.y=y2,l[0].b.x=x1,l[0].b.y=y1; n++; l[n].a.x=x2,l[n].a.y=y2,l[n].b.x=x2,l[n].b.y=y1; for(i=1;i<n;i++) { sum[i]=0; scanf("%d%d",&x1,&x2); l[i].a.x=x2;l[i].a.y=y2; l[i].b.x=x1;l[i].b.y=y1; }sum[0]=sum[n]=0; while(m--) { scanf("%d%d",&a.x,&a.y); if(cross(a,l[1].b,l[1].a)>0&&cross(a,l[0].b,l[0].a)<0) sum[0]++; else if(cross(a,l[n-1].b,l[n-1].a)<0&&cross(a,l[n].b,l[n].a)>0) sum[n-1]++; else { ans=s(a); if(cross(a,l[ans].b,l[ans].a)>0) sum[ans-1]++; else sum[ans]++; } } for(i=0;i<n;i++) printf("%d: %d\n",i,sum[i]); printf("\n"); } return 0; }