POJ 2318 TOYS 利用叉积判断点在线段的那一侧
题意:给定n(<=5000)条线段,把一个矩阵分成了n+1分了,有m个玩具,放在为位置是(x,y)。现在要问第几个位置上有多少个玩具。
思路:叉积,线段p1p2,记玩具为p0,那么如果(p1p2 ^ p1p0) (记得不能搞反顺序,不同的),如果他们的叉积是小于0,那么就是在线段的左边,否则右边。所以,可以用二分找,如果在mid的左边,end=mid-1 否则begin=mid+1。结束的begin,就是第一条在点右边的线段
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = +;
struct coor
{
double x,y;
coor(){}
coor(double xx,double yy):x(xx),y(yy){}
double operator ^(coor rhs) const //计算叉积(向量积)
{
return x*rhs.y - y*rhs.x;
}
coor operator -(coor rhs) const //坐标相减,a-b得到向量ba
{
return coor(x-rhs.x,y-rhs.y);
}
double operator *(coor rhs) const //数量积
{
return x*rhs.x + y*rhs.y;
}
};
struct Line
{
coor point1,point2;
Line(){}
Line(coor xx,coor yy):point1(xx),point2(yy){}
}a[maxn];
int n,m;
double xx1,yy1,xx2,yy2;
int cnt[maxn];
int calc (coor t)
{
int begin=,end=n;
while(begin<=end)
{
int mid = (begin+end)/;
coor ff1 = a[mid].point2 - a[mid].point1; //point1是起点
coor ff2 = t - a[mid].point1;
if ((ff1^ff2) < )
{
end=mid-;
}
else begin=mid+;
}
return begin;
}
void work ()
{
memset(cnt,,sizeof cnt);
for (int i=;i<=n;++i)
{
double xxx1,xxx2;
scanf("%lf%lf",&xxx1,&xxx2);
a[i].point1.x=xxx1, a[i].point1.y=yy1;
a[i].point2.x=xxx2, a[i].point2.y=yy2;
}
for (int i=;i<=m;++i)
{
coor t;
scanf("%lf%lf",&t.x,&t.y);
cnt[calc(t)-]++;
//printf ("%d\n",calc(t));
}
for (int i=;i<=n;++i)
{
printf ("%d: %d\n",i,cnt[i]);
}
return ;
}
int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
//while(~scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y) && (a.x+a.y+b.x+b.y)) work();
while(scanf("%d%d%lf%lf%lf%lf",&n,&m,&xx1,&yy1,&xx2,&yy2)!=EOF && n)
{
work();
printf ("\n");
}
return ;
}
POJ 2318 TOYS 利用叉积判断点在线段的那一侧的更多相关文章
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- poj 2318(叉积判断点在线段的哪一侧)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13120 Accepted: 6334 Description ...
- poj 2318 TOYS (二分+叉积)
http://poj.org/problem?id=2318 TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 101 ...
- POJ 2318 TOYS【叉积+二分】
今天开始学习计算几何,百度了两篇文章,与君共勉! 计算几何入门题推荐 计算几何基础知识 题意:有一个盒子,被n块木板分成n+1个区域,每个木板从左到右出现,并且不交叉. 有m个玩具(可以看成点)放在这 ...
- poj 2398(叉积判断点在线段的哪一侧)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5016 Accepted: 2978 Descr ...
- POJ 2318 TOYS (叉乘判断)
<题目链接> 题目大意: 给出矩形4个点和n个挡板俩顶点的位置,这n个挡板将该矩形分成 n+1块区域,再给你m个点的坐标,然你输出每个区域内有几个点. 解题思路: 用叉乘即可简单判断点与直 ...
- POJ 2318 TOYS (计算几何,叉积判断)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8661 Accepted: 4114 Description ...
- POJ 2318 TOYS (叉积+二分)
题目: Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...
- POJ 2318 TOYS 叉积
题目大意:给出一个长方形盒子的左上点,右下点坐标.给出n个隔板的坐标,和m个玩具的坐标,求每个区间内有多少个玩具. 题目思路:利用叉积判断玩具在隔板的左方或右方,并用二分优化查找过程. #includ ...
随机推荐
- EXPLAIN 命令
MySQL EXPLAIN 命令详解 MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提 ...
- (转)python set 用法
转载自:http://hi.baidu.com/����_xu/blog/item/5b9650c513bd3f049d163d8b .html python的set和其他语言类似, 是一个 基本功能 ...
- TS学习之解构与展开
一.解构 1.解构数组 let input = [1, 2]; let [first, second] = input; console.log(first); // outputs 1 consol ...
- BIOS MCSDK 2.0 User Guide - Acronyms and Definitions
BIOS MCSDK 2.0 User Guide - Texas Instruments Wiki Acronyms and Definitions The following acronyms a ...
- JDBC编程之程序优化
-----------------siwuxie095 首先下载 MySQL 的 JDBC 驱动,下载链接: https://dev.mysql.com/downloads/connector/j/ ...
- 《精通Spring4.X企业应用开发实战》读后感第五章(不同配置方式比较)
- 【BZOJ2839】集合计数 组合数+容斥
[BZOJ2839]集合计数 Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数 ...
- 浅谈.net的后台校验
1.场景描述 在开发中,前端的相关模型校验往往不能满足当前开发的需求,也就是并不是十分的安全.于是,很多情况下需要后端进行模型的校验.在.net mvc中,有很多校验的方式(比如:值(1)可以使用内置 ...
- 新一代web框架Koa源码学习
此文已由作者张佃鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Koa 就是一种简单好用的 Web 框架.它的特点是优雅.简洁.表达力强.自由度高.本身代码只有1000多行 ...
- scrapy-redis源码解读之发送POST请求
1 引言 这段时间在研究美团爬虫,用的是scrapy-redis分布式爬虫框架,奈何scrapy-redis与scrapy框架不同,默认只发送GET请求,换句话说,不能直接发送POST请求,而美团的数 ...