POJ 2318 - TOYS - [计算几何基础题]

题目链接:http://poj.org/problem?id=2318

Time Limit: 2000MS Memory Limit: 65536K

Description

Calculate the number of toys that land in each bin of a partitioned toy box. 
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys. 

John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box. 
 
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.

Input

The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1,y1) and (x2,y2), respectively. The following n lines contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.

Output

The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.

Sample Input

5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
 5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0

Sample Output

0: 2
1: 1
2: 1
3: 1
4: 0
5: 1

0: 2
1: 2
2: 2
3: 2
4: 2

 

题意:

给出n条“分割纸板”,将一个长方形纸盒分割成n+1个区间,编号为0~n;

给出m个玩具(每个玩具一个坐标,且必然严格落在区间内),问各个区域有多少个点。

 

题解:

加上纸盒左右两个边界,总共n+2条直线;

枚举每个玩具的坐标,二分判断在哪两条直线中间(由于题目给出直线的时候是按照从左往右的,所以不需要排序),这个区间的cnt+=1;

 

AC代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define MAX 5005
#define M_PI 3.14159265358979323846 //POJ的math头文件好像没有这个定义
using namespace std;
//--------------------------------------计算几何模板 - st--------------------------------------

const double eps = 1e-6;

struct Point{
    double x,y;
    Point(double tx=0,double ty=0):x(tx),y(ty){}
};
typedef Point Vctor;

//向量的加减乘除
Vctor operator + (Vctor A,Vctor B){return Vctor(A.x+B.x,A.y+B.y);}
Vctor operator - (Point A,Point B){return Vctor(A.x-B.x,A.y-B.y);}
Vctor operator * (Vctor A,double p){return Vctor(A.x*p,A.y*p);}
Vctor operator / (Vctor A,double p){return Vctor(A.x/p,A.y/p);}
bool operator < (Point A,Point B){return A.x < B.x || (A.x == B.x && A.y < B.y);}

struct Line{
    Point p;
    Vctor v;
    Line(Point p=Point(0,0),Vctor v=Vctor(0,0)):p(p),v(v){}
    Point point(double t){return p + v*t;} //获得直线上的距离p点t个单位长度的点
};
struct Circle{
    Point c;
    double r;
    Circle(Point tc=Point(0,0),double tr=0):c(tc),r(tr){}
    Point point(double a){return Point( c.x + cos(a)*r , c.y + sin(a)*r);}
};

int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return (x<0)?(-1):(1);
}
bool operator == (Point A,Point B){return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;}

//向量的点积,长度,夹角
double Dot(Vctor A,Vctor B){return A.x*B.x+A.y*B.y;}
double Length(Vctor A){return sqrt(Dot(A,A));}
double Angle(Vctor A,Vctor B){return acos(Dot(A,B)/Length(A)/Length(B));}

//叉积,三角形面积
double Cross(Vctor A,Vctor B){return A.x*B.y-A.y*B.x;}
double TriangleArea(Point A,Point B,Point C){return Cross(B-A,C-A);}

//向量的旋转,求向量的单位法线(即左转90度,然后长度归一)
Vctor Rotate(Vctor A,double rad){return Vctor( A.x*cos(rad) - A.y*sin(rad) , A.x*sin(rad) + A.y*cos(rad) );}
Vctor Normal(Vctor A)
{
    double L = Length(A);
    return Vctor(-A.y/L, A.x/L);
}

//直线的交点
Point getLineIntersection(Line L1,Line L2)
{
    Vctor u = L1.p-L2.p;
    double t = Cross(L2.v,u)/Cross(L1.v,L2.v);
    return L1.p + L1.v*t;
}

//点到直线的距离
double DistanceToLine(Point P,Line L)
{
    return fabs(Cross(P-L.p,L.v))/Length(L.v);
}

//点到线段的距离
double DistanceToSegment(Point P,Point A,Point B)
{
    if(A==B) return Length(P-A);
    Vctor v1 = B-A, v2 = P-A, v3 = P-B;
    if (dcmp(Dot(v1,v2)) < 0) return Length(v2);
    else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
    else return fabs(Cross(v1,v2))/Length(v1);
}

//点到直线的映射
Point getLineProjection(Point P,Line L)
{
    return L.v + L.v*Dot(L.v,P-L.p)/Dot(L.v,L.v);
}

//判断线段是否规范相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    double c1 = Cross(a2 - a1,b1 - a1), c2 = Cross(a2 - a1,b2 - a1),
           c3 = Cross(b2 - b1,a1 - b1), c4 = Cross(b2 - b1,a2 - b1);
    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}

//判断点是否在一条线段上
bool OnSegment(Point P,Point a1,Point a2)
{
    return dcmp(Cross(a1 - P,a2 - P))==0 && dcmp(Dot(a1 - P,a2 - P))<0;
}

//多边形面积
double PolgonArea(Point *p,int n)
{
    double area=0;
    for(int i=1;i<n-1;i++) area += Cross( p[i]-p[0] , p[i + 1]-p[0] );
    return area/2;
}

//判断圆与直线是否相交以及求出交点
int getLineCircleIntersection(Line L,Circle C,vector<Point> &sol)
{
    double t1,t2;
    double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
    double e = a*a + c*c , f = 2*(a*b + c*d),  g = b*b + d*d - C.r*C.r;
    double delta = f*f - 4.0*e*g;
    if(dcmp(delta)<0) return 0;
    else if(dcmp(delta)==0)
    {
        t1 = t2 = -f/(2.0*e);
        sol.push_back(L.point(t1));
        return 1;
    }
    else
    {
        t1 = (-f-sqrt(delta))/(2.0*e); sol.push_back(L.point(t1));
        t2 = (-f+sqrt(delta))/(2.0*e); sol.push_back(L.point(t2));
        return 2;
    }
}

//判断并求出两圆的交点
double angle(Vctor v){return atan2(v.y,v.x);}
int getCircleIntersection(Circle C1,Circle C2,vector<Point> &sol)
{
    double d = Length(C1.c - C2.c);
    //圆心重合
    if(dcmp(d)==0)
    {
        if(dcmp(C1.r-C2.r)==0) return -1; //两圆重合
        else return 0; //包含关系
    }

    //圆心不重合
    if(dcmp(C1.r+C2.r-d)<0) return 0; // 相离
    if(dcmp(fabs(C1.r-C2.r)-d)>0) return 0; // 包含

    double a = angle(C2.c - C1.c);
    double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d));
    Point p1 = C1.point(a - da), p2 = C1.point(a + da);
    sol.push_back(p1);
    if(p1==p2) return 1;
    sol.push_back(p2);
    return 2;
}

//求点到圆的切线
int getTangents(Point p,Circle C,vector<Line> &sol)
{
    Vctor u=C.c-p;
    double dis=Length(u);
    if(dis<C.r) return 0;
    else if(dcmp(dis-C.r) == 0)
    {
        sol.push_back(Line(p,Rotate(u,M_PI/2)));
        return 1;
    }
    else
    {
        double ang=asin(C.r/dis);
        sol.push_back(Line(p,Rotate(u,-ang)));
        sol.push_back(Line(p,Rotate(u,ang)));
        return 2;
    }
}

//求两圆的切线
int getCircleTangents(Circle A,Circle B,Point *a,Point *b)
{
    int cnt = 0;
    if(A.r<B.r){swap(A,B);swap(a,b);}
    //圆心距的平方
    double d2 = (A.c.x - B.c.x)*(A.c.x - B.c.x) + (A.c.y - B.c.y)*(A.c.y - B.c.y);
    double rdiff = A.r - B.r;
    double rsum = A.r + B.r;
    double base = angle(B.c - A.c);
    //重合有无限多条
    if(d2 == 0 && dcmp(A.r - B.r) == 0) return -1;
    //内切
    if(dcmp(d2-rdiff*rdiff) == 0)
    {
        a[cnt] = A.point(base);
        b[cnt] = B.point(base);
        cnt++;
        return 1;
    }
    //有外公切线
    double ang = acos((A.r - B.r) / sqrt(d2));
    a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
    a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;

    //一条内切线
    if(dcmp(d2-rsum*rsum) == 0)
    {
        a[cnt] = A.point(base);
        b[cnt] = B.point(M_PI + base);
        cnt++;
    }//两条内切线
    else if(dcmp(d2-rsum*rsum) > 0)
    {
        double ang = acos((A.r + B.r) / sqrt(d2));
        a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
        a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
    }
    return cnt;
}

//--------------------------------------计算几何模板 - ed--------------------------------------

int n,m;
Point upper_left,lower_right;
Line x_axis;//下边界
Line cardboard[MAX];//隔板
Point toy;
struct Area{
    int id,cnt;
}area[MAX];

int Left_of_Line(Line l,Point p)
{
    if(Cross(l.v,p-l.p)>0) return 1;//左边
    else return 0;
}
int where(Point toy)
{
    int l=0,r=n+1;
    while(r-l>1)
    {
        int mid=(l+r)/2;
        if(Left_of_Line(cardboard[mid],toy)) r=mid;
        else l=mid;
    }
    return l;
}

int main()
{
    while(scanf("%d",&n) && n!=0)
    {
        scanf("%d%lf%lf%lf%lf",&m,&upper_left.x,&upper_left.y,&lower_right.x,&lower_right.y);

        x_axis=Line(lower_right,Vctor(-1,0));
        cardboard[0]=Line(Point(upper_left.x,lower_right.y),Vctor(0,1)), cardboard[n+1]=Line(lower_right,Vctor(0,1));

        Point U=Point(0,upper_left.y),L=Point(0,lower_right.y);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&U.x,&L.x);
            //printf("(%lf,%lf) (%lf,%lf)\n",U.x,U.y,L.x,L.y);
            cardboard[i]=Line(L,U-L);
            //printf("Line %d: p=(%lf,%lf) v=(%lf,%lf)\n",i,cardboard[i].p.x,cardboard[i].p.y,cardboard[i].v.x,cardboard[i].v.y);
        }

        for(int i=0;i<=n;i++) area[i]=(Area){i,0};
        for(int i=1;i<=m;i++)
        {
            scanf("%lf%lf",&toy.x,&toy.y);
            area[where(toy)].cnt++;
        }
        for(int i=0;i<=n;i++) printf("%d: %d\n",area[i].id,area[i].cnt);
        printf("\n");
    }
}

 

posted @ 2017-11-01 16:41  Dilthey  阅读(357)  评论(0编辑  收藏  举报