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 ...
随机推荐
- MOOK学习
课程选择及其理由 课程:c++程序设计 教师:魏英 学校:西北工业大学 总共:48讲 选择理由:我其实之前找了好几个,但由于小白,思考了下(迷茫,感觉好像都不错),然后看了一下大家都选择了西北工业大学 ...
- HDU 5875 Function 优先队列+离线
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...
- Rsyslog初步学习
一.Rsyslog整体架构 Rsyslog消息流:输入模块——>预处理模块——>主队列——>过滤模块——>执行队列——>输出模块 1. 输入模块 输入模块是消息来源 2. ...
- 201621123037 《Java程序设计》第1周学习总结
#作业01-Java基本概念 1. 本周学习总结 本周学习内容JDK JVM JRE 跨平台 .java .class 关键概念之间的联系: 总的来讲JDK是给开发人员们提供专门用来开发的环境,并且包 ...
- webpack打包多html开发案例新
闲来无事在原来简单打包案例的基础上,参考vue-cli的打包代码,改为多文件打包. 区别于上篇文章<webpack打包多html开发案例>,此次打包根据开发的不同环节进行打包,也就是有开发 ...
- Cobbler环境搭建
Cobbler服务器系统: CentOS 6.6 64位Cobbler版本: cobbler-2.6.11IP地址:192.168.166.136 1.安装epel库 rpm -ivh http:// ...
- 【Linux学习笔记】Linux C中内联汇编的语法格式及使用方法(Inline Assembly in Linux C)
http://blog.csdn.net/slvher/article/details/8864996 https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm. ...
- iOS 关于MVC和MVVM设计模式的那些事
一.概述 在 iOS 开发中,MVC(Model View Controller)是构建iOS App的标准模式,是苹果推荐的一个用来组织代码的权威范式.Apple甚至是这么说的.在MVC下,所有的对 ...
- urllib2 request 模拟伪装浏览器
直接上代码吧 # -*- coding:utf-8 -*- import urllib2 import random url = "http://www.baidu.com/" # ...
- bzoj1853-大包子的幸运数字
题意 称只含有 6 和 8 的数字为幸运数字.称幸运数字的倍数为类幸运数字.求 \([l,r]\) 中有多少个类幸运数字.\(1\le l,r\le 10^{10}\) . 分析 幸运数字最多有 \( ...