【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 ...
随机推荐
- mybatis数据库数据分页问题
http://www.cnblogs.com/jcli/archive/2011/08/09/2132222.html 借花献佛,天天进步
- 国内5款优秀的WEB前端框架
1. JX(腾讯) 官网地址:http://alloyteam.github.io/JX/#home JX 是一个类似 Google Closure Library 的 Web 前端开发框架,服务于 ...
- linux之SQL语句简明教程---SUBSTRING
SQL 中的 substring 函数是用来抓出一个栏位资料中的其中一部分.这个函数的名称在不同的资料库中不完全一样: MySQL: SUBSTR( ), SUBSTRING( ) Oracle: S ...
- Signal ()函数详细介绍 Linux函数
http://blog.csdn.net/ta893115871/article/details/7475095 Signal ()函数详细介绍 Linux函数 signal()函数理解 在<s ...
- Parallelogram Counting(平行四边形个数,思维转化)
1058 - Parallelogram Counting PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...
- 奇妙的算法之LCS妙解
LCS算法妙解 LCS问题简述:最长公共子序列 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则S 称为已知序列的最长公共子序列. LCS问题的分支:最长公共子串 ...
- 2014/4月金山WPS笔试
今晚去參加了金山的笔试. 一開始还以为选C++的人不会非常多. 我去啊,一去到,好多人,一整个大教室都快满人了. 还好我算是去的比較早的了. 还拿到了一个位置. 金山还是挺不错的,对于我这类还没有实力 ...
- 设置TextView水平居中显示
1.让TextView里的内容水平居中 android:gravity="center_horizontal" 2.让TextView控件在它的父布局里水平居中 android:l ...
- (转)ASP.NET缓存概念及其应用浅析
ASP.NET缓存概念及其应用浅析 ASP.NET缓存是什么呢?ASP.NET缓存有什么样子的特点呢?本文就向你详细介绍ASP.NET缓存的相关情况. ASP.NET缓存概念是什么呢?通常,应用程序可 ...
- 常用的CSS清除浮动的方法优缺点分析(个人学习笔记)
一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class="outer"> <div class="di ...