poj2318TOYS

来源:互联网 发布:淘宝游戏交易平台 编辑:程序博客网 时间:2024/05/22 10:38

判断每个分块有几个玩具,可以用二分的方法,再就是叉积的基本应用了。

#include<cstdio>#include<cstring>using namespace std;const int maxn = 5010;struct point{    double x,y;    point (double x=0, double y=0):x(x),y(y) {}};double xmult(point p1,point p2,point p0){    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}double x1,y1,x2,y2;int n,m;double xx1[maxn],xx2[maxn];int que[maxn];int main(){    int f=1;    while(~scanf("%d",&n)&&n)    {        if(f)f=0;        else printf("\n");        memset(que,0,sizeof que);        scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);        for(int i=0; i<n; i++)        {            scanf("%lf %lf",&xx1[i],&xx2[i]);        }        xx1[n]=x2,xx2[n]=x2;        int tmp;        for(int i=0; i<m; i++)        {            point a;            scanf("%lf%lf",&a.x,&a.y);            int l=0,r=n,mid;            while(l<=r)            {                mid = (l+r)/2;//                printf("%d %lf %lf\n",mid,xx1[mid],xx2[mid]);                point b = point(xx1[mid],y1) ,c = point(xx2[mid],y2);                if(xmult(a,b,c) < 0)                    tmp = mid ,r =  mid - 1;                else                    l = mid + 1;            }            que[tmp]++;        }        for(int i=0; i<=n; i++)            printf("%d: %d\n",i,que[i]);    }    return 0;}


0 0
原创粉丝点击