判断点在直线的左侧还是右侧
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
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.
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 0 3 1 4 3 6 8 10 10 15 30 1 5 2 1 2 8 5 5 40 10 7 9 4 10 0 10 100 0 20 20 40 40 60 60 80 80 5 10 15 10 25 10 35 10 45 10 55 10 65 10 75 10 85 10 95 10 0Sample Output
0: 2 1: 1 2: 1 3: 1 4: 0 5: 1 0: 2 1: 2 2: 2 3: 2 4: 2Hint
As the example illustrates, toys that fall on the boundary of the box are "in" the box.
题目分析 : 给你一个箱子,将箱子用一些木板分隔开,在询问一些点,问这些点在哪好箱子中
思路分析 : 叉积 + 二分
代码示例 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | const int maxn = 1e4+5; #define ll long long int n, m, x11, y11, x22, y22; struct node { int u, l; }pre[maxn]; int ans[maxn]; int x, y; bool ok( int k) { int x1 = pre[k].u-pre[k].l; int y1 = y11 - y22; int x2 = x - pre[k].l; int y2 = y - y22; int f1 = x1*y2-x2*y1; if (f1 > 0) return true ; else return false ; } void search(){ int l = 0, r = n+1; while (l <= r){ int mid = (l+r) >> 1; if (ok(mid)) r=mid-1; else l=mid+1; } ans[r]++; } int main() { while ( scanf ( "%d" , &n) && n){ scanf ( "%d%d%d%d%d" , &m, &x11, &y11, &x22, &y22); int k = 1; for ( int i = 1; i <= n; i++){ scanf ( "%d%d" , &pre[i].u, &pre[i].l); } memset (ans, 0, sizeof (ans)); pre[0].u = pre[0].l = x11; pre[n+1].u = pre[n+1].l = x22; for ( int i = 1; i <= m; i++){ scanf ( "%d%d" , &x, &y); search(); } for ( int i = 0; i <= n; i++){ printf ( "%d: %d\n" , i, ans[i]); } printf ( "\n" ); } return 0; } |
作者:静默虚空
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
东北日出西边雨 道是无情却有情
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!