POJ2318 TOYS
嘟嘟嘟
题面:先告诉你一个矩形抽屉的坐标,然后\(n\)个隔板将抽屉分成了\(n + 1\)格(格子从\(0\)到\(n - 1\)标号),接下来随机输入\(m\)个玩具的坐标。问最后每一个格子里有多少个玩具。
仔细想想就是一道计算几何入门题。
对于一个玩具\((x_0, y_0)\),我们只要找到在他的左面且离他最近的隔板\(i\),则这个玩具就在第\(i\)格里。
怎么判断隔板\(AB\)(规定\(A\)点是隔板的上端,\(B\)点是隔板下端)是否在玩具的左边?用叉积即可:只要\(\overrightarrow{OA} \times \overrightarrow{OB} > 0\),就说明在左边。
然后因为有人说从左到右枚举会超时,所以我就把枚举改成倍增了。
然鹅枚举\(O(n ^2)\)能过呀,于是我就枚举交了一发,也过了……(???)
代码里注释掉的是枚举的
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e3 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << 1) + (ans << 3) + ch - '0'; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, xl, yl, xr, yr;
int ans[maxn];
struct Vec
{
int x, y;
int operator * (const Vec& oth)const
{
return x * oth.y - oth.x * y;
}
};
struct Point
{
int x, y;
Vec operator - (const Point& oth)const
{
return (Vec){x - oth.x, y - oth.y};
}
}ad[maxn], au[maxn], P;
const int N = 13;
void solve()
{
int L = 0;
for(int i = N, t; i >= 0; --i) if((t = L + (1 << i)) <= n)
{
if((au[t] - P) * (ad[t] - P) > 0) L = t;
}
/*int L = 0;
while(L < n)
{
if((au[L + 1] - P) * (ad[L + 1] - P) < 0) break;
L++;
}*/
ans[L]++;
}
int main()
{
while(scanf("%d", &n) && n)
{
Mem(ans, 0);
m = read();
xl = read(); yl = read(); xr = read(); yr = read();
au[0].y = yl;
for(int i = 1; i <= n; ++i)
{
au[i].x = read(); au[i].y = yl;
ad[i].x = read(); ad[i].y = yr;
}
for(int i = 1; i <= m; ++i)
{
P.x = read(); P.y = read();
solve();
}
for(int i = 0; i <= n; ++i) write(i), putchar(':'), space, write(ans[i]), enter;
enter;
}
return 0;
}
POJ2318 TOYS的更多相关文章
- POJ2318 TOYS[叉积 二分]
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14433 Accepted: 6998 Description ...
- POJ2318 TOYS(叉积判断点与直线的关系+二分)
Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a prob ...
- POJ-2318 TOYS,暴力+叉积判断!
TOYS 2页的提交记录终于搞明白了. 题意:一个盒子由n块挡板分成n+1块区 ...
- POJ-2318 TOYS 计算几何 判断点在线段的位置
题目链接:https://cn.vjudge.net/problem/POJ-2318 题意 在一个矩形内,给出n-1条线段,把矩形分成n快四边形 问某些点在那个四边形内 思路 二分+判断点与位置关系 ...
- [poj2318]TOYS(直线与点的位置关系)
解题关键:计算几何入门题,通过叉积判断. 两个向量的关系: P*Q>0,Q在P的逆时针方向: P*Q<0,Q在P的顺时针方向: P*Q==0,Q与P共线. 实际就是用右手定则判断的. #i ...
- POJ2318:TOYS(叉积判断点和线段的关系+二分)&&POJ2398Toy Storage
题目:http://poj.org/problem?id=2318 题意: 给定一个如上的长方形箱子,中间有n条线段,将其分为n+1个区域,给定m个玩具的坐标,统计每个区域中的玩具个数.(其中这些线段 ...
- 【kuangbin专题】计算几何基础
1.poj2318 TOYS 传送:http://poj.org/problem?id=2318 题意:有m个点落在n+1个区域内.问落在每个区域的个数. 分析:二分查找落在哪个区域内.叉积判断点与线 ...
- poj分类解题报告索引
图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...
- POJ2318:TOYS——题解
http://poj.org/problem?id=2318 题目大意:给一个大矩形,分成n+1份,求落在每一份的点的数量. —————————————————— 首先叉积可以判断一个点在边界的左边还 ...
随机推荐
- SQL Server中的小技巧(重复、替换、截取、去空格、去小数点后的位数)
PS:随笔写的在SQL Server中要用到的 (重复.替换.截取.去空格.去小数点后的位数) /*---------------------------重复--------------------- ...
- C#可选参数、命名参数、参数数组
学习了C#4.0的新特性:可选参数.命名参数.参数数组. 1.可选参数,是指给方法的特定参数指定默认值,在调用方法时可以省略掉这些参数. 但要注意: (1)可选参数不能为参数列表的第1个参数,必须位于 ...
- [C#]跨模块的可选参数与常量注意事项
假设某个DLL里有这么一个类: // Lib.dll public class Lib { public const string VERSION = "1.0"; public ...
- 经典的SQL面试题及答案
- scrum 项目的基本模式
scrum 项目中有 3 个主要的角色:产品所有者, Scrum 主管和团队成员 产品所有者和团队其他成员一起工作,负责维护生产积压工作表 (production backlog) ,并对表中的项制定 ...
- Q:java中的泛型数组
对于java,其是不支持直接创建泛型数组的.当采用如下的方式去创建一个泛型数组时,其会出现错误,编译无法通过的情况. package other.jdk1_5; /** * 该类用于演示泛型数组的创 ...
- webapi views目录下html文件无法访问
找到views下web.config 增加如下红色标注内容 <?xml version="1.0"?> <configuration> <config ...
- Spring的大框架
初识Spring: Spring作者:Rod Johnson Spring框架由20个模块组成,这些模块分成六个部分,分别是Core Container,Data Access/Integration ...
- 当react 项目使用px2rem
参考资料:http://easywork.xin/2018/09/02/react-2/ 我拿到的设计图 是 375px //配置px2rem px2rem({remUnit: 37.5}) 在 ...
- mustache 模板,用于构造html页面内容
Mustache 的模板语法很简单,就那么几个: {{data}} {{#data}} {{/data}} {{^data}} {{/data}} {{.}} {{<partials}} {{{ ...