POJ 2318 (叉积) TOYS
题意:

有一个长方形,里面从左到右有n条线段,将矩形分成n+1个格子,编号从左到右为0~n。
端点分别在矩形的上下两条边上,这n条线段互不相交。
现在已知m个点,统计每个格子中点的个数。
分析:
用叉积判断点与线段的相对位置,对于每个点二分查找所在的格子。
#include <cstdio>
#include <cmath>
#include <cstring> struct Point
{
int x, y;
Point(int x=, int y=):x(x), y(y) {}
};
typedef Point Vector; Point read_point()
{
int x, y;
scanf("%d%d", &x, &y);
return Point(x, y);
} Point operator + (const Point& A, const Point& B)
{ return Point(A.x+B.x, A.y+B.y); } Point operator - (const Point& A, const Point& B)
{ return Point(A.x-B.x, A.y-B.y); } int Cross(const Point& A, const Point& B)
{ return A.x*B.y - A.y*B.x; } const int maxn = + ;
int up[maxn], down[maxn], ans[maxn];
int n, m, kase = ;
Point A0, B0; int binary_search(const Point& P)
{
int L = , R = n;
while(L < R)
{
int mid = L + (R - L + ) / ;
Point A(down[mid], B0.y), B(up[mid], A0.y);
Vector v1 = B - A;
Vector v2 = P - A;
if(Cross(v1, v2) < ) L = mid;
else R = mid - ;
}
return L;
} int main()
{
//freopen("in.txt", "r", stdin); while(scanf("%d", &n) == && n)
{
memset(ans, , sizeof(ans)); scanf("%d", &m); A0 = read_point(); B0 = read_point();
for(int i = ; i <= n; ++i) scanf("%d%d", &up[i], &down[i]);
for(int i = ; i < m; ++i)
{
Point P; P = read_point();
int pos = binary_search(P);
ans[pos]++;
} if(kase++) puts("");
for(int i = ; i <= n; ++i) printf("%d: %d\n", i, ans[i]);
} return ;
}
代码君
POJ 2318 (叉积) TOYS的更多相关文章
- poj 2318 叉积+二分
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13262 Accepted: 6412 Description ...
- (POJ 2318)TOYS 向量叉积
题目链接:http://poj.org/problem?id=2318 #include<stdio.h> #include<cstdlib> #include<cstr ...
- 【POJ 2318】TOYS 叉积
用叉积判断左右 快速读入写错了卡了3小时hhh #include<cmath> #include<cstdio> #include<cstring> #includ ...
- poj 2318(叉积判断点在线段的哪一侧)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13120 Accepted: 6334 Description ...
- POJ 2318 叉积判断点与直线位置
TOYS Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom ...
- 「POJ - 2318」TOYS (叉乘)
BUPT 2017 summer training (16) #2 A 题意 有一个玩具盒,被n个隔板分开成左到u右n+1个区域,然后给每个玩具的坐标,求每个区域有几个玩具. 题解 依次用叉积判断玩具 ...
- POJ 2318/2398 叉积性质
2318 2398 题意:给出n条线将一块区域分成n+1块空间,再给出m个点,询问这些点在哪个空间里. 思路:由于只要求相对位置关系,而对具体位置不关心,那么易使用叉积性质得到相对位置关系(左侧/右侧 ...
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage
POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...
随机推荐
- linux下rm误删除数据库文件的恢复方法
在linux redhat 5.4版本,rm误删除数据库文件的恢复过程分享.测试没有问题,可用. 1.首先测试rm 误删除数据库文件 [oracle@primary dbwdn]$ ll total ...
- python开源项目及示例代码
本页面是俺收集的各种 Python 资源,不定期更新. 下面列出的各种 Python 库/模块/工具,如果名称带超链接,说明是第三方的:否则是 Python 语言内置的. 1 算法 1.1 字符串处理 ...
- Linux学习系列之Linux入门(二)Vim学习
第二篇 Vim学习 主要内容: 基本命令: 插件扩展: 参考资料: vim是一个命令控制的文本编辑器,可以完成几乎我们想要做的所有工作,除了Emacs几乎没有其他的工具能和它匹敌.官方网站是:http ...
- 《WPF程序设计指南》读书笔记——第6章 Dock与Grid
1.DockPanel面板 using System; using System.Windows; using System.Windows.Controls; using System.Window ...
- 【转】#ifdef __cplusplus深度剖析
原文:http://bbs.ednchina.com/BLOG_ARTICLE_251752.HTM 时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus extern ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- Hello World for U (20)
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...
- 【转】perl特殊符号及默认的内部变量
perl特殊符号及默认的内部变量,有需要的朋友不妨参考下 Perl的特殊符号 @ 数组 $x{} x名字前面是美元符号($),后面是花 ...
- 每日一记:搭建Memcached + php 缓存系统
服务器环境,Centos6.5 1.安装Memcached服务端 Yum -y install memcached 2.配置Memcached服务端用户以及自动启动服务等 将服务配置成自启动 chkc ...
- Understanding Responsive Web Design: Cross-browser Compatibility
http://www.sitepoint.com/understanding-responsive-web-design-cross-browser-compatibility/ In the las ...