poj 2398 Toy Storage(计算几何 点线关系)

Toy Storage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4588   Accepted: 2718

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

Source

与poj 2318差不多,就是多了个对直线排序~以及输出时按格子内玩具数量递减输出,没有的就不输出~

题意:还是给定一个矩形,里面若干线,保证不相交,再给若干点,判断这些点都在哪些区域~

ps:输出实例之间不需要空格~

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <math.h>
  7 #include <algorithm>
  8 #include <cctype>
  9 #include <string>
 10 #include <map>
 11 #define N 500015
 12 #define INF 1000000
 13 #define ll long long
 14 using namespace std;
 15 struct Point
 16 {
 17     int x,y;
 18     Point(){}
 19     Point(int _x,int _y)
 20     {
 21         x = _x;y = _y;
 22     }
 23     Point operator -(const Point &b)const
 24     {
 25         return Point(x - b.x,y - b.y);
 26     }
 27     int operator *(const Point &b)const
 28     {
 29         return x*b.x + y*b.y;
 30     }
 31     int operator ^(const Point &b)const
 32     {
 33         return x*b.y - y*b.x;
 34     }
 35 };
 36 struct Line
 37 {
 38     Point s,e;
 39     Line(){}
 40     Line(Point _s,Point _e)
 41     {
 42         s = _s;e = _e;
 43     }
 44 };
 45 
 46 int xmult(Point p0,Point p1,Point p2) //计算p0p1 X p0p2
 47 {
 48     return (p1-p0)^(p2-p0);
 49 }
 50 
 51 const int MAXN = 5050;
 52 Line line[MAXN];
 53 int ans[MAXN];
 54 int num[MAXN];
 55 bool cmp(Line a,Line b)
 56 {
 57     return a.s.x  < b.s.x;
 58 }
 59 
 60 int main(void)
 61 {
 62     int n,m,x1,y1,x2,y2,i;
 63     int ui,li;
 64     while(scanf("%d",&n),n)
 65     {
 66         scanf("%d %d %d %d %d",&m,&x1,&y1,&x2,&y2);
 67         for(i = 0; i < n; i++)
 68         {
 69             scanf("%d%d",&ui,&li);
 70             line[i] = Line(Point(ui,y1),Point(li,y2));
 71         }
 72         line[n] = Line(Point(x2,y1),Point(x2,y2));
 73         sort(line,line+n+1,cmp);
 74         int x,y;
 75         Point p;
 76         memset(ans,0,sizeof(ans));
 77 
 78         while(m--)
 79         {
 80             scanf("%d %d",&x,&y);
 81             p = Point(x,y);
 82             int l = 0,r = n,tmp = 0;
 83             while(l <= r)
 84             {
 85                 int mid = (l + r)/2;
 86                 if(xmult(p,line[mid].s,line[mid].e) < 0)
 87                 {
 88                     tmp = mid;
 89                     r = mid - 1;
 90                 }
 91                 else
 92                     l = mid + 1;
 93             }
 94             ans[tmp]++;
 95         }
 96         memset(num,0,sizeof(num));
 97         for(i = 0; i <= n; i++)
 98             if(ans[i] > 0)
 99             num[ans[i]]++;
100         printf("Box
");
101         for(i = 1; i <= n; i++)
102             if(num[i] > 0)
103             printf("%d: %d
",i,num[i]);
104     }
105     return 0;
106 }
原文地址:https://www.cnblogs.com/henserlinda/p/4735653.html