【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 ...
随机推荐
- android 测量控件视图的方法
在实际项目中经常要用到 测量一个控件或者视图的高,宽.然后根据这个高宽进行一些逻辑. 计算视图宽高有几种方式先简单的了解下android 视图的绘制过程会促进理解. 一.android View绘制过 ...
- struts2配置文件struts.xml的简介
本文在于总结,深入研究有别人写的很好了,也没必要再去写,将在本文后面附上他们的文章地址: 一.struts2的执行过程: 二.struts2的配置文件struts.xml 下面是其三大部分includ ...
- delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)(通过修改OpenTool生效)
delphi 的IDE快捷键与输入法切换键中突,以往的解决方法是下载一个ImeTool修改 windows 系统的快捷键 在 xp win7 都好使,但在win 10经常是修改完后,重启又失效了. 本 ...
- docker 镜像管理
docker:/root# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official bui ...
- KnockOutJS学习系列----(一)
原文地址:http://www.cnblogs.com/n-pei/archive/2011/12/23/2299217.html 好几个月没去写博客了,最近也是因为项目紧张,不过这个不是借口,J. ...
- ID3决策树算法原理及C++实现(其中代码转自别人的博客)
分类是数据挖掘中十分重要的组成部分.分类作为一种无监督学习方式被广泛的使用. 之前关于"数据挖掘中十大经典算法"中,基于ID3核心思想的分类算法C4.5榜上有名.所以不难看出ID3 ...
- 滚动视差效果——background-attachment
滚动视差效果的实现原理是在同一个页面上将页面元素分为多层,例如可以分为背景.内容.贴图层,在滚动页面的时候让三者滚动的速度不一,从而在人的视觉上能够形成一种立体的近似效果.最近在做一个项目wiki的时 ...
- C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库
第一步:首先需要将csv先装换成datatable,这样我们就容易进行对datatable进行遍历: /// 将CSV文件的数据读取到DataTable中 /// CSV文件路径 /// 返回读取了C ...
- 阻止JS事件冒泡传递(cancelBubble 、stopPropagation)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- IOS开发 统计XCODE 代码行数
如果要统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 find . -name "*.m" -or -name "*.h" -or -nam ...