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 }
时间: 2024-10-15 16:54:06

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

poj 2398 Toy Storage (计算几何,判断点和线段关系)

http://poj.org/problem?id=2398 题意大概是说将一个盒子用n个board分成n+1 部分 然后往里面放toy,给定盒子,board,和toy的坐标 问所有的toy放完后,有多少部分中有t个toy; 简单计算几何 需要判断的是点和直线的关系. 判断 某一点在直线左右侧 左右方向是相对前进方向的,只要指定了前进方向就可以知道左右(比如指定前进方向是从直线的起点到终点).判断点在直线的左侧还是右侧是计算几何里面的一个最基本算法.使用矢量来判断. 定义:平面上的三点P1(x1

poj 2318 TOYS &amp; poj 2398 Toy Storage (叉积)

链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段,并且这些线段坐标是按照顺序给出的, 有n条线段,把盒子分层了n+1个区域,然后有m个玩具,这m个玩具的坐标是已知的,问最后每个区域有多少个玩具 分析:从左往右,直到判断玩具是否在线段的逆时针方向为止,这个就需要用到叉积,当然可以用二分查找优化. 叉积:已知向量a(x1,y1),向量b(x2,y2),axb=x1*y2-x2*y1, 若axb>0,a在b的逆时针方向,若axb<0,则a在b的顺时针方向 注:每组数据后要多空一行

POJ 2398 Toy Storage 叉积 和2318基本一样

B - Toy Storage Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2398 Appoint description:  System Crawler  (2016-05-10) Description Mom and dad have a problem: their child, Reza, never puts his

POJ 2398 Toy Storage(叉积+二分)

题目链接:POJ 2398 Toy Storage 之前做的类似题目:POJ 2318 TOYS [题意]跟之前做的POJ 2318差不多额,给你一个矩形,有被若干直线分成N个格子,给出M个点(玩具)的坐标,问你放有t个玩具的格子的个数. [思路]其实跟POJ 2318差不多,利用叉积+二分,但是本题中直线的输入不是按顺序的,要进行排序,才能做二分.开始写错了构造函数,然后就一直不对啊,看来C++的学习之路还很远啊. 1 /* 2 ** POJ 2398 Toy Storage 3 ** Cre

poj 2398 Toy Storage【二分+叉积】

二分点所在区域,叉积判断左右 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; const int N=1005; int T,n,m,x1,y1,x2,y2,ans[N],cnt[N]; struct dian { double x,y; dian(double X=0,d

POJ 2398 - Toy Storage

Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5439   Accepted: 3234 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

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

poj 2398 Toy Storage (计算几何)

A - Toy Storage Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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 rec

poj 2398 Toy Storage 【计算几何】【点和线的关系】

题目链接:http://poj.org/problem?id=2398 题目大意:这次的题目和前一道题目几乎是一样的,不同之处在于这次给出的线不是有顺序的,还有就是输出的时候有一个优化. 基本的分析见我上篇博客:http://blog.csdn.net/u010468553/article/details/39474007 注意sort函数中cmp的编写还有后期数据的整理 #include<iostream> #include<stdio.h> #include<cstrin