码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2398 - Toy Storage - [计算几何基础题][同POJ2318]

时间:2017-11-03 22:04:31      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:sample   problem   size   storage   sort   partition   logs   efi   color   

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

Time Limit: 1000MS Memory Limit: 65536K

Description

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 0
20 20
80 80
60 60
40 40
5 10
15 10
95 10
25 10
65 10
75 10
35 10
45 10
55 10
85 10
5 6 0 10 60 0
4 3
15 30
3 1
6 8
10 10
2 1
2 8
1 5
5 5
40 10
7 9
0

Sample Output

Box
2: 5
Box
1: 4
2: 1

 

与POJ 2318几乎一模一样的题。

POJ 2318的题解:http://www.cnblogs.com/dilthey/p/7767218.html

由于本题输入cardboard的时候是乱序,所以在二分前需要sort一下;另外输出的方式和2318不太一样,改一下即可。

 

AC代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#define MAX 5005
#define M_PI 3.14159265358979323846 //POJ的math头文件好像没有这个定义
using namespace std;

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=0,Vctor v=Vctor(0,0)):p(p),v(v){}
    Point point(double t){return p + v*t;} //获得直线上的距离p点t个单位长度的点
};

double Cross(Vctor A,Vctor B){return A.x*B.y-A.y*B.x;}

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;}

int n,m;
Point upper_left,lower_right;
Line x_axis;//下边界
Line cardboard[MAX];//隔板
Point toy;
int area[MAX],cnt[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;
}

bool cmp(Line a,Line b)
{
    if(a.p==b.p) return (a.p+a.v)<(b.p+b.v);
    else return a.p<b.p;
}
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);
            cardboard[i]=Line(L,U-L);
        }
        sort(cardboard+1,cardboard+n+1,cmp);

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

 

POJ 2398 - Toy Storage - [计算几何基础题][同POJ2318]

标签:sample   problem   size   storage   sort   partition   logs   efi   color   

原文地址:http://www.cnblogs.com/dilthey/p/7780166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!