POJ 2318/2398 叉积性质
题意:给出n条线将一块区域分成n+1块空间,再给出m个点,询问这些点在哪个空间里。
思路:由于只要求相对位置关系,而对具体位置不关心,那么易使用叉积性质得到相对位置关系(左侧/右侧),再因为是简单几何线段不相较,即有序分布,那么在求在哪个区间时可以先对所有线段根据x坐标排序,使用二分减少复杂度。
/** @Date : 2017-07-11 11:05:59
* @FileName: POJ 2318 叉积性质.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct Point
{
int x, y;
Point(){}
Point(int xx, int yy){x = xx, y = yy;}
Point operator -(const Point &b) const
{
return Point(x - b.x, y - b.y);
}
int operator *(const Point &b) const
{
return x * b.x + y * b.y;
}
}; int cross(Point a, Point b)
{
return a.x * b.y - a.y * b.x;
} struct Line
{
Point s, t;
Line(){}
Line(Point ss, Point tt){s = ss, t = tt;}
}; int JudegeCross(Point p0, Point p1, Point p2)
{
return cross(p1 - p0, p2 - p0);
} Line li[N];
int ans[N];
int vis[N];
int cmp(Line a, Line b)
{
return a.s.x < b.s.x;
} int main()
{
int n, m, x1, x2, y1, y2;
while(~scanf("%d", &n) && n)
{
MMF(ans);
MMF(vis);
scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
for(int i = 0; i < n; i++)
{
int s, t;
scanf("%d%d", &s, &t);
li[i] = Line(Point(s, y1), Point(t, y2));
}
li[n] = Line(Point(x2, y1), Point(x2, y2));
sort(li, li + n + 1, cmp);
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
Point p = Point(x, y);
int l = 0, r = n;
int pos = 0;
while(l <= r)
{
int mid = (l + r) >> 1;
if(JudegeCross(p, li[mid].s, li[mid].t) < 0)
{
pos = mid;
r = mid - 1;
}
else
l = mid + 1;
}
ans[pos]++;
}
printf("Box\n");
for(int i = 0; i <= n; i++)
if(ans[i])
vis[ans[i]]++;
for(int i = 1; i <= n; i++)
if(vis[i])
printf("%d: %d\n", i, vis[i]);
}
return 0;
}
/** @Date : 2017-07-11 11:05:59
* @FileName: POJ 2318 叉积性质.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct Point
{
int x, y;
Point(){}
Point(int xx, int yy){x = xx, y = yy;}
Point operator -(const Point &b) const
{
return Point(x - b.x, y - b.y);
}
int operator *(const Point &b) const
{
return x * b.x + y * b.y;
}
}; int cross(Point a, Point b)
{
return a.x * b.y - a.y * b.x;
} struct Line
{
Point s, t;
Line(){}
Line(Point ss, Point tt){s = ss, t = tt;}
}; int JudegeCross(Point p0, Point p1, Point p2)
{
return cross(p1 - p0, p2 - p0);
} Line li[N];
int ans[N]; int main()
{
int n, m, x1, x2, y1, y2;
while(~scanf("%d", &n) && n)
{
MMF(ans);
scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
for(int i = 0; i < n; i++)
{
int s, t;
scanf("%d%d", &s, &t);
li[i] = Line(Point(s, y1), Point(t, y2));
}
li[n] = Line(Point(x2, y1), Point(x2, y2));
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
Point p = Point(x, y);
int l = 0, r = n;
int pos = 0;
while(l <= r)
{
int mid = (l + r) >> 1;
if(JudegeCross(p, li[mid].s, li[mid].t) < 0)
{
pos = mid;
r = mid - 1;
}
else
l = mid + 1;
}
ans[pos]++;
}
for(int i = 0; i <= n; i++)
{
printf("%d: %d\n", i, ans[i]);
}
printf("\n");
}
return 0;
}
POJ 2318/2398 叉积性质的更多相关文章
- 二分+叉积判断方向 poj 2318 2398
// 题意:问你每个区域有多少个点 // 思路:数据小可以直接暴力 // 也可以二分区间 #include <cstdio> #include <cstring> #inclu ...
- POJ 2318 TOYS 叉积
题目大意:给出一个长方形盒子的左上点,右下点坐标.给出n个隔板的坐标,和m个玩具的坐标,求每个区间内有多少个玩具. 题目思路:利用叉积判断玩具在隔板的左方或右方,并用二分优化查找过程. #includ ...
- POJ 2318 TOYS (叉积+二分)
题目: Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...
- 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage
POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...
- poj 2318 TOYS & poj 2398 Toy Storage (叉积)
链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...
- 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage
题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
- poj 2318 叉积+二分
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13262 Accepted: 6412 Description ...
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- poj 2398(叉积判断点在线段的哪一侧)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5016 Accepted: 2978 Descr ...
随机推荐
- snprintf vs sprintf
#include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream, const char * ...
- SDN前瞻 传统网络的缺陷
引言 在网络发展速度如此之快的今天,传统网络的架构充满了危机,主要有这四个问题: 传统网络部署管理困难. 分布式架构瓶颈出现. 流量控制难真正实现. 设备不可编程. 现在的网络厂商 种类繁多的网络厂商 ...
- 关于C语言的问卷调查!!!!!!!!!!
1.我对自己的未来是现在通过大学这一平台逐渐接触社会,通过大学的这段时间学习C语言等计算机语言技术,有一技之长在手,并且通过大学时间丰富自己的业余生活,加强自己的人脉关系,为未来在事业上的发展做准备! ...
- 【技术向】rainmeter的设计与发现
我们在大学期间所学的那点代码知识还远远不够,于是我就自己寻找到了一款简单易懂的软件,来丰富我的代码知识. 这款软件叫rainmeter,中文叫做雨滴,是一款可以修改桌面的软件.它可以将桌面上更改出硬盘 ...
- ansible的介绍和一些基本模块介绍
必须保证ansible工作站与各个node实现无密码ssh登入 ①:192.168.1.100 - 在你本地的工作站或服务器上安装 Ansible. ②:文件服务器1到代理服务器3 - 使用 19 ...
- 小程序获取 openid 和 session_key
<?php //获取openid function getopenid(){//获取用户ID //code为前端通过 wx.login() 方式获取 $code = $_GET["co ...
- sublime text there are no packages available for installation 解决办法
hosts 增加一行 : #50.116.33.29 sublime.wbond.net.
- BZOJ3560 DZY Loves Math V(欧拉函数)
对每个质因子分开计算再乘起来.使用类似生成函数的做法就很容易统计了. #include<iostream> #include<cstdio> #include<cmath ...
- CPP 替代 PIL 图片处理(缩略图生成)
python中使用PIL(Pyhton Image Library)进行图片处理,好处就是编写简单方便,但是不能很好利用机器多核的特点,于是在项目中决定使用cpp来实现图片处理. 项目中的图片处理主要 ...
- 802.1p 优先级与内部优先级的映射关系
缺省情况下,所有华为 S 系列交换机的 802.1P 优先级 与内部优先级的映射关系是 一 样的,如表 10-3 所示.从中可以看出,这些交换机中 802.1p 优先级与内部优先级的缺省映射关系是按等 ...