前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2398--Toy Storage(叉积判断,二分找点,点排序)

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

作者头像
Enterprise_
发布2019-02-20 10:47:36
2790
发布2019-02-20 10:47:36
举报
文章被收录于专栏:小L的魔法馆

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

  • 题意:给定n条无顺序的边,将一个矩形划分成n+1个区域,再给定m个点,求每个区域各有多少个点,输出将按区域内存在的点的数目进行升序排序。 和POJ2318类似,但是这里的点需要排序,还有输出结果不同
  • Code
代码语言:javascript
复制
#include<iostream>
#include<cstring> 
#include<string>
#include<algorithm> 
#include<cstdio> 
#include<cstdlib> 
#include<cmath> 
using namespace std;
const int MAX = 5005;
typedef struct point {
    int x;
    int y;
}point;
typedef struct value {
    point start;
    point end;
}v;
v edge[MAX];
int sum[MAX], ans[MAX];
int n, m, x1, y11, x2, y2, flag = 1,Ui, Li;
point tp;
int Xj, Yj;
bool com(const v t1, const v t2) {
    return t1.start.x < t2.start.x;
}
bool com2(const int a, const int b) {
    return a < b;
}
int multi(point p1, point p2, point p0) {  //判断p1p0和p2p0的关系,<0,p1p0在p2p0的逆时针方向
    return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
}
void inset(point p) {
    int low = 0, high = n;
    while (low <= high) {
        int mid = (high + low) / 2;
        if (multi(p, edge[mid].start, edge[mid].end) < 0)   /*点p在边的左侧*/
            high = mid - 1;
        else    //点p在边的右侧
            low = mid + 1;
    }
    if (multi(p, edge[low-1].start, edge[low-1].end) < 0 )
        sum[low-1]++;
    else
        sum[low]++;
}
int main() {
    while (cin>>n && n) {
        memset(sum, 0, sizeof(sum));
        memset(ans, 0, sizeof(ans));
        cin >> m >> x1 >> y11 >> x2 >> y2;
        for (int i = 0; i < n; i++) {
            cin >> Ui >> Li;
            edge[i].start.x = Ui;
            edge[i].start.y = y11;
            edge[i].end.x = Li;
            edge[i].end.y = y2;
        }
        edge[n].start.x = x2;
        edge[n].start.y = y11;
        edge[n].end.x = x2;
        edge[n].end.y = y2;
        sort(edge, edge + n + 1, com);
        for (int j = 0; j < m; j++) {
            cin >> Xj >> Yj;
            tp.x = Xj;
            tp.y = Yj;
            inset(tp);
        }
        for (int i = 0; i <= n; i++)
        {
            if (sum[i] != 0)
                ans[sum[i]]++;
        }
        cout << "Box" << endl;
        for (int i = 0; i <= n; i++)
        {
            if (ans[i] != 0)
                cout << i << ": " << ans[i] << endl;
        }   
    }
    return 0;
}
  • 再熟悉一下叉积函数
代码语言:javascript
复制
int multi(point p1, point p2, point p0) { 
    return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
}
 //判断p1p0和p2p0的关系
 //结果<0, p1p0在p2p0的逆时针方向,即点p1在p2p0的左侧
 //结果>0, p1p0在p2p0的顺时针方向,即点p1在p2p0的右侧
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年07月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档