【POJ】2318 TOYS ——计算几何+二分
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 10281 | Accepted: 4924 |
Description
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
Output
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
0
Sample Output
0: 2
1: 1
2: 1
3: 1
4: 0
5: 1 0: 2
1: 2
2: 2
3: 2
4: 2
Hint
#include <cstdio>
#include <cstring> const int LEN = ; struct Point //点的结构体
{
int x;
int y;
}; struct Line //线段的结构体
{
Point a;
Point b;
}line[LEN]; int ans[LEN]; int judge(Line tline, Point p3) //运用叉积的性质判断点在线段的左边还是右边
{
Point t1, t2;
t1.x = p3.x - tline.b.x;
t1.y = p3.y - tline.b.y;
t2.x = tline.a.x - tline.b.x;
t2.y = tline.a.y - tline.b.y;
return t1.x * t2.y - t1.y * t2.x;
} int divide(int l, int r, Point toy) //二分查找
{
int mid = (l + r) / ;
if (mid == l)
return l;
if (judge(line[mid], toy) < )
return divide(l, mid, toy);
else
return divide(mid, r, toy);
} int main()
{
int n, m, x1, y1, x2, y2;
//freopen("in.txt", "r", stdin);
while(scanf("%d", &n) != EOF && n){
scanf("%d %d %d %d %d", &m, &x1, &y1, &x2, &y2);
memset(ans, , sizeof(ans));
for(int i = ; i <= n; i++){
int up, low;
scanf("%d %d", &up, &low);
line[i].a.x = up;
line[i].a.y = y1;
line[i].b.x = low;
line[i].b.y = y2;
}
n++;
line[].a.x = line[].b.x = x1;
line[].b.y = line[n].b.y = y2;
line[].a.y = line[n].a.y = y1;
line[n].a.x = line[n].b.x = x2;
for(int i = ; i < m; i++){
Point toy;
scanf("%d %d", &toy.x, &toy.y);
ans[divide(, n, toy)]++;
}
for(int i = ; i < n; i++){
printf("%d: %d\n", i, ans[i]);
}
printf("\n");
}
return ;
}
【POJ】2318 TOYS ——计算几何+二分的更多相关文章
- 2018.07.03 POJ 2318 TOYS(二分+简单计算几何)
TOYS Time Limit: 2000MS Memory Limit: 65536K Description Calculate the number of toys that land in e ...
- POJ 2318 TOYS (叉积+二分)
题目: Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...
- poj 2318 TOYS(计算几何 点与线段的关系)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12015 Accepted: 5792 Description ...
- POJ 2318 TOYS(计算几何)
跨产品的利用率推断点线段向左或向右,然后你可以2分钟 代码: #include <cstdio> #include <cstring> #include <algorit ...
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- poj 2318 TOYS (二分+叉积)
http://poj.org/problem?id=2318 TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 101 ...
- 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage
题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
- POJ 2318 TOYS && POJ 2398 Toy Storage(几何)
2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...
- 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage
POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...
随机推荐
- TensorFlow深度学习笔记 循环神经网络实践
转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 加 ...
- 轻松背后的N+疲惫——系统日志
相信很多coder都有这样的癖好:“自恋”!!对自己编写的code总是那么的自信,自豪,Always believe it to be so perfect!! 不喜欢做单元测试(总觉得它就那样了能出 ...
- MYSQL delete 从多人表中删除
语法 1. delete table_list_A from table_list_B where bool_expression; 从delete_table_list_A 删除 bool expr ...
- javascript对象继承的实现
现在有两个对象,需要实现Chinese类型对象对Person类型对象的继承. 这里分两部分,属性和方法. 属性可以直接用构造函数的方法实现继承,而方法则要通过原型链来实现继承. 先解释什么是原型链,每 ...
- (十一)boost库之多线程间通信
(十一)boost库之多线程间通信 1.互斥锁 在编程中,引入了对象互斥锁的概念,来保证共享数据操作的完整性.每个对象都对应于一个可称为" 互斥锁" 的标记,这个标记用来保证在任一 ...
- libevent evbuffer bug
今天发现 libevent 2.0.22 一个坑爹的bug,导致消息混乱.查找问题浪费一天,复现代码如下 #include <event2/buffer.h> #include <s ...
- BLOB存储图片文件二进制数据是非对错
子在一天一天虚度,生活也在一天一天中茫然 做人做事哪能尽如人意,付出多少收获多少虽然存在偏颇,但是不劳而获的心态是万万不对的,更不能去怨天尤人,低调为人.做好自己就可以了 改进你的系统的最好的方法是先 ...
- PHP转码函数
解决中文乱码问题,数据库使用sql server ,是gbk编码,所以在存储和前台显示时都要转码. function cn_convert2web($str){ return iconv('gbk', ...
- 一道来自华为的C机试题目
题目是这样的 求一个字符串中连续字母的个数 比如I have a book. : 1 I have a dog. : 0 I haavee aa dogg : 4 #include <windo ...
- 设置ListView的item多选
一,首先定义一个类来关联Adapter和Activity能够记住点击的位置 public class MyViewHoler{ public CheckBox cb; public TextView ...