POJ 2398 Toy Storage (叉积判断点和线段的关系)

题目链接

Toy Storage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4104   Accepted: 2433

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

题意:

告诉一个矩形,在告诉n条直线的信息,这n条直线把这个矩形划分成n+1个区域,接着告诉m个点的坐标,

升序输出包含非零的 i 个点的区域有多少个。

输入是无序的,需要自己排序

分析:

暴搜一遍,用叉积判断点是在线段的左侧还是右侧。

 

还有一种做法是枚举线段,然后对点排序以后,二分点,这种二分的做法比较快。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 #define LL __int64
 8 const int maxn = 1e3 + 10;
 9 const double eps = 1e-8;
10 using namespace std;
11 struct node
12 {
13     double x, y;
14 }p[maxn];
15 struct line
16 {
17     double u, l;
18 }li[maxn];
19 bool cmp(line a, line b)
20 {
21     return a.u < b.u;
22 }
23 double cross(node a, node b, node c)
24 {
25     return ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y));
26 }
27 
28 int main()
29 {
30     int n, m, i, j, f[maxn], d[maxn];
31     double x1, x2, y1, y2;
32     while(~scanf("%d", &n)&&n)
33     {
34         scanf("%d", &m);
35         memset(f, 0, sizeof(f));
36         memset(d, 0, sizeof(d));
37         scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
38         for(i = 0; i < n; i++)
39         {
40             scanf("%lf%lf", &li[i].u, &li[i].l);
41         }
42         li[i].u = x2; li[i].l = x2;
43         n ++;
44         sort(li, li+n, cmp);
45         for(i = 0; i < m; i++)
46         {
47             scanf("%lf%lf", &p[i].x, &p[i].y);
48         }
49         int cnt = 0;
50         for(i = 0; i < n; i++)
51         {
52             node t1, t2;
53             t1.x = li[i].u; t1.y = y1;
54             t2.x = li[i].l; t2.y = y2;
55             int sum = 0;
56             for(j = 0; j < m; j++)
57             {
58                 if(cross(t1, t2, p[j])<=0)
59                 sum ++;
60             }
61             if(i == 0) d[i] = sum;
62             else d[i] = sum-cnt;
63             f[d[i]] ++;
64             cnt = sum;
65         }
66         cout<<"Box"<<endl;
67         for(i = 1; i <= m; i++)
68         {
69             if(f[i])
70             printf("%d: %d\n", i, f[i]);
71         }
72     }
73     return 0;
74 }

 

posted @ 2014-11-26 14:51  水门  阅读(213)  评论(0编辑  收藏  举报