博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2318 TOYS(点与凸多边形位置关系)
阅读量:5060 次
发布时间:2019-06-12

本文共 6528 字,大约阅读时间需要 21 分钟。

题目链接:

  

题目描述:

TOYS

Description

Calculate the number of toys that land in each bin of a partitioned toy box. 
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John 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 John to find his favorite toys. 
John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box. 
 
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.

Input

The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). 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 contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.

Output

The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.

Sample Input

5 6 0 10 60 03 14 36 810 1015 301 52 12 85 540 107 94 10 0 10 100 020 2040 4060 6080 80 5 1015 1025 1035 1045 1055 1065 1075 1085 1095 100

Sample Output

0: 21: 12: 13: 14: 05: 10: 21: 22: 23: 24: 2

Hint

As the example illustrates, toys that fall on the boundary of the box are "in" the box.

 

题目大意:

  一个矩形内被隔板分成若干区域,给出点坐标,判断在哪个区域内

思路:

  把隔板化作向量,由底边指向上边,第i条边对应的向量记为 ve[i]

  第i条边的底边上的点指向p的向量记为 tmp

  判断点p与第i条边的位置关系,只需要看 tmp 与 ve[i] 的叉乘的正负即可

  叉乘为正说明 tmp 在 ve[i] 左侧,反之在右侧

  

  最后二分求解

 

代码:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 9 const int N = 5010;10 11 struct Point {12 double x, y;13 Point(double x = 0, double y = 0) :x(x), y(y) {}14 }; //点的定义15 16 typedef Point Vector; //向量的定义17 18 Vector operator - (Vector& A, Vector& B) { return Vector(A.x - B.x, A.y - B.y); } //向量减法19 20 double Cross(Vector& A, Vector& B) { return A.x*B.y - A.y*B.x; } //向量叉乘21 22 int cnt[N], X1, Y1, X2, Y2, n, m, id = 0, L[N];23 Vector ve[N];24 25 int calc(Point& p, int l, int r) { //二分26 if (l == r)return l;27 int mid = (l + r) >> 1;28 Vector tmp(p.x - L[mid], p.y - Y2);29 if (Cross(tmp, ve[mid]) < 0)return calc(p, l, mid);30 else return calc(p, mid + 1, r);31 }32 33 int main() {34 int u, l;35 while (cin >> n&&n) {36 memset(cnt, 0, sizeof(cnt));37 scanf("%d%d%d%d%d", &m, &X1, &Y1, &X2, &Y2);38 ve[n].x = 0, ve[n].y = Y1 - Y2;39 L[n] = X1;40 for (int i = 0; i < n; ++i) {41 scanf("%d%d", &u, &l);42 L[i] = l;43 ve[i].x = u - l, ve[i].y = Y1 - Y2;44 }45 Point tmp;46 for (int i = 0; i < m; ++i) {47 scanf("%lf%lf", &tmp.x, &tmp.y);48 ++cnt[calc(tmp, 0, n)];49 }50 if (id++)printf("\n");51 for (int i = 0; i <= n; ++i)52 printf("%d: %d\n", i, cnt[i]);53 }54 }

 

 

TOYS
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15103   Accepted: 7291

Description

Calculate the number of toys that land in each bin of a partitioned toy box. 
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John 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 John to find his favorite toys. 
John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box. 
 
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.

Input

The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). 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 contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.

Output

The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.

Sample Input

5 6 0 10 60 03 14 36 810 1015 301 52 12 85 540 107 94 10 0 10 100 020 2040 4060 6080 80 5 1015 1025 1035 1045 1055 1065 1075 1085 1095 100

Sample Output

0: 21: 12: 13: 14: 05: 10: 21: 22: 23: 24: 2

Hint

As the example illustrates, toys that fall on the boundary of the box are "in" the box.

转载于:https://www.cnblogs.com/hyp1231/p/6985812.html

你可能感兴趣的文章
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
关于js sort排序方法
查看>>
JAVA面试常见问题之Redis篇
查看>>
javascript:二叉搜索树 实现
查看>>
网络爬虫Heritrix源码分析(一) 包介绍
查看>>
__int128的实现
查看>>
Problem - 1118B - Codeforces(Tanya and Candies)
查看>>
jdk1.8 api 下载
查看>>
svn 图标不显示
查看>>
getElement的几中属性介绍
查看>>
iOS 使用Quartz 2D画虚线 【转】
查看>>
平面最接近点对
查看>>
HTML列表,表格与媒体元素
查看>>
PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点?
查看>>
雨林木风 GHOST_XP SP3 快速装机版YN12.08
查看>>
linux基础-命令
查看>>
java对象的深浅克隆
查看>>
Hadoop流程---从tpch到hive
查看>>
数据结构3——浅谈zkw线段树
查看>>