POJ2398 Toy Storage 判断点和线的关系+二分+计数

来源:互联网 发布:选择公理 知乎 编辑:程序博客网 时间:2024/05/22 00:54

Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza 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 Reza to find his favorite toys anymore. 
Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top: 

We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.
Input
The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). 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 each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard. 

A line consisting of a single 0 terminates the input.
Output
For each box, first provide a header stating "Box" on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.
Sample Input
4 10 0 10 100 020 2080 8060 6040 405 1015 1095 1025 1065 1075 1035 1045 1055 1085 105 6 0 10 60 04 315 303 16 810 102 12 81 55 540 107 90
Sample Output
Box2: 5Box1: 42: 1


#include <iostream>#include <cstdio>#include <map>#include <set>#include <vector>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstring>#include <string>using namespace std;#define INF 0x3f3f3f3f#define CVector CPointtypedef long long LL;double PI=acos(-1);double ESP=1e-6;struct CPoint{    double x,y;    CPoint(double xx,double yy):x(xx),y(yy){}    CPoint(){}};struct CLine{    CPoint a,b;    CLine(CPoint aa,CPoint bb):a(aa),b(bb){}    CLine(){}};CPoint operator +(CPoint a,CPoint b){    return CPoint(a.x+b.x,a.y+b.y);}//c=a+bCPoint operator -(CPoint a,CPoint b){    return CPoint(a.x-b.x,a.y-b.y);}//c=a-bCPoint operator *(CPoint a,double k){    return CPoint(k*a.x,k*a.y);}//c=k*a;CPoint operator *(double k,CPoint a){    return CPoint(k*a.x,k*a.y);}//c=k*adouble operator *(CPoint a,CPoint b){    return a.x*b.x+a.y*b.y;}//c=a*b 点击double operator ^(CPoint a,CPoint b){    return a.x*b.y-a.y*b.x;}//叉积double Length(CVector p){    return sqrt(p*p);}//求长度CVector unit(CVector p){    return 1.0/Length(p)*p;}//求p方向单位向量double project(CVector p,CVector n){    return p*(unit(n));}//求p在n方向上投影长度double area(CVector a,CVector b){    return a^b*0.5;}//求a,b所夹的有向面积bool isZero(double x){    return -ESP<x&&x<ESP;}//判断是否为0double dist(CPoint p,CPoint q){    return Length(p-q);}//求两点间距离double dist(CPoint p,CLine l){    return fabs((p-l.a)^(p-l.b))/Length(l.a-l.b);}//求点到线的距离CPoint Rotate(CPoint a,CPoint b,double alpha){    CVector p=b-a;    return CPoint(a.x+(p.x*cos(alpha)                       -p.y*sin(alpha)),                  a.y+(p.x*sin(alpha)                       +p.y*cos(alpha)));}//b绕a逆时针旋转alphaint sideOfLine(CPoint p,CLine l){    double res=(p-l.a)^(p-l.b);    if(isZero(res)){        return 0;    }else{        return res>0? 1:-1;    }}//判断点和直线关系,0-直线上,1-左,-1-右CLine Vertical(CPoint p,CLine l){    return CLine(p,p+(Rotate(l.b,l.a,PI/2)-l.a));}//求过p的l的垂线CPoint root(CPoint p,CLine l){    return l.a+project(p-l.a,l.b-l.a)*unit(l.b-l.a);}//垂足double angle(CLine l,CLine m){    return acos(fabs(                project(l.b-l.a,m.b-m.a)                /Length(l.b-l.a)));}//两直线夹角CLine s[1005];int num[1005],cnt[1005];bool cmp(CLine a,CLine b){    return a.a.x<b.a.x&&a.b.x<b.b.x;}int main(){    int n,m;    double x1,x2,y1,y2;    while(~scanf("%d",&n),n){        int maxn=0;        scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);        memset(num,0,sizeof(num));        memset(cnt,0,sizeof(cnt));        s[0].a=CPoint(x1,y1);        s[0].b=CPoint(x1,y2);        for(int i=1;i<=n;i++){            double x_1,x_2;            scanf("%lf%lf",&x_1,&x_2);            s[i].a=CPoint(x_1,y1);            s[i].b=CPoint(x_2,y2);        }        sort(s+1,s+1+n,cmp);        s[n+1].a=CPoint(x2,y1);        s[n+1].b=CPoint(x2,y2);        while(m--){            double x,y;            scanf("%lf%lf",&x,&y);            CPoint temp(x,y);            int l=0,r=n+1;            int _m=(l+r)/2;            while(r-l>1){                if(sideOfLine(temp,s[_m])==-1){                    r=_m;                }else{                    l=_m;                }                _m=(l+r)/2;            }            if(num[_m]){                cnt[num[_m]]--;            }            num[_m]++;            cnt[num[_m]]++;            maxn=max(maxn,num[_m]);        }        printf("Box\n");        for(int i=1;i<=maxn;i++){            if(cnt[i]){                printf("%d: %d\n",i,cnt[i]);            }        }    }    return 0;}


0 0
原创粉丝点击