POJ 2398--Toy Storage(叉积判断,二分找点,点排序)

Toy Storage

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6534   Accepted: 3905

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

Tehran 2003 Preliminary

  • 题意:给定n条无顺序的边,将一个矩形划分成n+1个区域,再给定m个点,求每个区域各有多少个点,输出将按区域内存在的点的数目进行升序排序。 
    POJ2318类似,但是这里的点需要排序,还有输出结果不同
  • code:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<algorithm>
     5 #include<cstdio>
     6 #include<cstdlib>
     7 #include<cmath>
     8 using namespace std;
     9 const int MAX = 5005;
    10 typedef struct point {
    11     int x;
    12     int y;
    13 }point;
    14 typedef struct value {
    15     point start;
    16     point end;
    17 }v;
    18 v edge[MAX];
    19 int sum[MAX], ans[MAX];
    20 int n, m, x1, y11, x2, y2, flag = 1,Ui, Li;
    21 point tp;
    22 int Xj, Yj;
    23 bool com(const v t1, const v t2) {
    24     return t1.start.x < t2.start.x;
    25 }
    26 bool com2(const int a, const int b) {
    27     return a < b;
    28 }
    29 int multi(point p1, point p2, point p0) {  //判断p1p0和p2p0的关系,<0,p1p0在p2p0的逆时针方向
    30     return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
    31 }
    32 void inset(point p) {
    33     int low = 0, high = n;
    34     while (low <= high) {
    35         int mid = (high + low) / 2;
    36         if (multi(p, edge[mid].start, edge[mid].end) < 0)    /*点p1在边的左侧*/
    37             high = mid - 1;
    38         else    //点p在边的右侧
    39             low = mid + 1;
    40     }
    41     if (multi(p, edge[low-1].start, edge[low-1].end) < 0 )
    42         sum[low-1]++;
    43     else
    44         sum[low]++;
    45 }
    46 int main() {
    47     while (cin>>n && n) {
    48         memset(sum, 0, sizeof(sum));
    49         memset(ans, 0, sizeof(ans));
    50         cin >> m >> x1 >> y11 >> x2 >> y2;
    51         for (int i = 0; i < n; i++) {
    52             cin >> Ui >> Li;
    53             edge[i].start.x = Ui;
    54             edge[i].start.y = y11;
    55             edge[i].end.x = Li;
    56             edge[i].end.y = y2;
    57         }
    58         edge[n].start.x = x2;
    59         edge[n].start.y = y11;
    60         edge[n].end.x = x2;
    61         edge[n].end.y = y2;
    62         sort(edge, edge + n + 1, com);
    63         for (int j = 0; j < m; j++) {
    64             cin >> Xj >> Yj;
    65             tp.x = Xj;
    66             tp.y = Yj;
    67             inset(tp);
    68         }
    69         for (int i = 0; i <= n; i++)
    70         {
    71             if (sum[i] != 0)
    72                 ans[sum[i]]++;
    73         }
    74         cout << "Box" << endl;
    75         for (int i = 0; i <= n; i++)
    76         {
    77             if (ans[i] != 0)
    78                 cout << i << ": " << ans[i] << endl;
    79         }
    80     }
    81     return 0;
    82 }

  • 再熟悉一下叉积函数

    1 再熟悉一下叉积函数
    2 int multi(point p1, point p2, point p0) {
    3     return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
    4 }
    5  //判断p1p0和p2p0的关系
    6  //结果<0, p1p0在p2p0的逆时针方向,即点p1在p2p0的左侧
    7  //结果>0, p1p0在p2p0的顺时针方向,即点p1在p2p0的右侧

原文地址:https://www.cnblogs.com/FlyerBird/p/9270719.html

时间: 2024-10-28 04:56:14

POJ 2398--Toy Storage(叉积判断,二分找点,点排序)的相关文章

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 (计算几何,判断点和线段关系)

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

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 (叉积判断点和线段的关系)

题目链接 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 rectangula

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 - [计算几何基础题][同POJ2318]

题目链接: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