TOYS(计算几何基础+点与直线的位置关系)
题目链接:http://poj.org/problem?id=2318
题面:
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 17413 | Accepted: 8300 |
Description
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.
John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.

For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.
Input
Output
Sample Input
5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0
Sample Output
0: 2
1: 1
2: 1
3: 1
4: 0
5: 1 0: 2
1: 2
2: 2
3: 2
4: 2
Hint
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 5e3 + ;
int n, m, x1, x2, y1, y2;
int num[maxn]; struct node {
int x, y;
bool operator < (const node &b) const {
return x < b.x;
}
} P[maxn], L[maxn], nw, nxt; //P储存所访问的点(其实也可以不用数组的),L储存直线(对于直线此处的x为上方的x,y为下方的y); double dot(node a, node b) {
return (a.x * b.y - a.y * b.x);
} bool check(node p, node pp) {
nw.x = p.x - pp.y, nw.y = p.y - y2, nxt.x = pp.x - pp.y, nxt.y = y1 - y2;
if( dot(nw, nxt) < ) { //为负则说明当前访问的点在该直线的左端;
return true;
} else
return false;
} void Throw(node p) {
for(int i = ; i < n; i++) {
if(check(p, L[i])) {
num[i]++;
return;
}
}
num[n]++;
return;
} int main() {
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n) && n) {
scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
memset(num, , sizeof(num));
for(int i = ; i < n; i++) {
scanf("%d%d", &L[i].x, &L[i].y);
}
sort(L, L + n);
for(int i = ; i < m; i++) {
scanf("%d%d", &P[i].x, &P[i].y);
Throw(P[i]);
}
for(int i = ; i <= n; i++) {
printf("%d: %d\n", i, num[i]);
}
printf("\n");
}
return ;
}
18年10月6日更新:
前面的代码是以前看题解写的,今天再做一次,发现就是求一个叉积的事。
思路:首先将所有直线用结构体存起来,按照u排序,若u相同则按照l排序;这样就使得线段是有序的,第1条线段左边是0号区域,第2条左边是1号……第n条左边是n-1号,右边是n号。将每个点依次与这些直线求叉积,若该点在第i条直线的顺时针方向(也就是叉积为正数),那么点必落在第i-1号区域;若与所有直线的叉积都为负数则落在第n号区域。
代码实现如下:
#include <set>
#include <map>
#include <deque>
#include <ctime>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define name2str(name)(#name)
#define bug printf("**********\n");
#define IO ios::sync_with_stdio(false);
#define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
#define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin); const double eps = 1e-;
const int maxn = + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, m, x1, yy, x2, y2, x, y;
int cnt[maxn]; struct Line {
int l, r;
bool operator < (const Line& x) const {
return l == x.l ? r < x.r : l < x.l;
}
}L[maxn]; int cross(int x1, int y1, int x2, int y2, int x3, int y3) {
return (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
int vis = ;
while(~scanf("%d", &n) && n) {
if(vis) printf("\n");
vis = ;
scanf("%d%d%d%d%d", &m, &x1, &yy, &x2, &y2);
for(int i = ; i <= n; i++) {
scanf("%d%d", &L[i].l, &L[i].r);
}
sort(L + , L + n + );
memset(cnt, , sizeof(cnt));
for(int i = ; i <= m; i++) {
scanf("%d%d", &x, &y);
int flag = ;
for(int j = ; j <= n; j++) {
if(cross(L[j].r, y2, L[j].l, yy, x, y) > ) {
flag = ;
cnt[j-]++;
break;
}
}
if(!flag) cnt[n]++;
}
for(int i = ; i <= n; i++) {
printf("%d: %d\n", i, cnt[i]);
}
}
return ;
}
TOYS(计算几何基础+点与直线的位置关系)的更多相关文章
- Intersecting Lines (计算几何基础+判断两直线的位置关系)
题目链接:http://poj.org/problem?id=1269 题面: Description We all know that a pair of distinct points on a ...
- poj 1269 判断直线的位置关系
题目链接 题意 判断两条直线的位置关系,重合/平行/相交(求交点). 直线以其上两点的形式给出(点坐标为整点). 思路 写出直线的一般式方程(用\(gcd\)化为最简), 计算\(\begin{vma ...
- Intersecting Lines---poj1269(求两直线的位置关系)
题目链接:http://poj.org/problem?id=1269 题意:给你两条直线上的任意不同的两点,然后求两条直线的位置关系,如果相交于一点输出该点坐标; #include<iostr ...
- 判断两条直线的位置关系 POJ 1269 Intersecting Lines
两条直线可能有三种关系:1.共线 2.平行(不包括共线) 3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...
- POJ 1269 /// 判断两条直线的位置关系
题目大意: t个测试用例 每次给出一对直线的两点 判断直线的相对关系 平行输出NODE 重合输出LINE 相交输出POINT和交点坐标 1.直线平行 两向量叉积为0 2.求两直线ab与cd交点 设直线 ...
- POJ 2318 - TOYS - [计算几何基础题]
题目链接:http://poj.org/problem?id=2318 Time Limit: 2000MS Memory Limit: 65536K Description Calculate th ...
- POJ 2318 /// 判断点与直线的位置关系
题目大意: n块玩具箱隔板 m个玩具落地点 给定玩具箱的左上和右下两个端点 接下来给定n块隔板的上点的x和下点的x(因为y就是玩具箱的上下边缘) 接下来给定m个玩具落地点 输出n+1个区域各有的玩具数 ...
- POJ 2398 map /// 判断点与直线的位置关系
题目大意: poj2318改个输出 输出 a: b 即有a个玩具的格子有b个 可以先看下poj2318的报告 用map就很方便 #include <cstdio> #include < ...
- 【kuangbin专题】计算几何基础
1.poj2318 TOYS 传送:http://poj.org/problem?id=2318 题意:有m个点落在n+1个区域内.问落在每个区域的个数. 分析:二分查找落在哪个区域内.叉积判断点与线 ...
随机推荐
- LintCode-366.斐波纳契数
斐波纳契数列 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是:0, 1, 1 ...
- ejabberd学习2
1.ejabberd监听多个端口 每个网络连接进来,ejabberd都会使用一个进程来负责这个连接的数据处理.原理跟Joe Armstrong的<Erlang程序设计>中的并行服务器一样, ...
- 团队组队&灰化肥挥发会发黑
1. 队伍展示 (1. 队名: 灰化肥挥发会发黑 (2. 队员风采 苏叶潇(队长) 201521123114 与众不同,擅长软件测试,对编程望而却步,希望成为测试人员. 宣言:不求最好,只求更好. 李 ...
- linux后台运行之screen和nohup
3.1 nohup命令 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令. 该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. nohup就是不挂起的意 ...
- Mysql查询优化从入门到跑路(三)查询的基本操作
查询的基本操作 1.选择操作 对应的是限制条件,操作对象是二维表的行. 优化方式:选择操作下推 目的:尽量减少连接操作前的元租数,使得中间临时关系尽量少(元祖数少,连接得到的元组数就少 ...
- 第71天:jQuery基本选择器(二)
jQuery选择器 一.内容过滤选择器 选择器 描 述 返 回 示 例 :contains(text) 匹配含有文本内容text的元素 集合元素 $(“p:contains(今天)”) :empty ...
- JQuery UI的拖拽功能实现方法小结
JQuery UI提供的API极大简化了拖拽功能的开发.只需要分别在拖拽源(source)和目标(target)上调用draggable和droppable两个函数即可. 拖拽原理 首先要明确几个概念 ...
- 三节点搭建openstack-Mitaka版本
前言: 现在的云计算平台已经非常火,也非常的稳定了.像阿里云平台,百度云平台等等,今天咱们基于openstack来搭建一个云平台 注意: 本次平台搭建为三节点搭建(没有外部存储节点,所有存储为本地存储 ...
- Python高级数据类型模块collections
collections模块提供更加高级的容器数据类型,替代Python的内置dict,list, set,和tuple Counter对象 提供计数器,支持方便和快速的计数.返回的是一个以元素为键, ...
- 蒟蒻Orion还要学的东西!
这个ID多元化真是个麻烦的事情...... 一会KamijouIndex一会dedicatus545一会Orion的,乱死了啊啊啊啊 数据结构 圆方树 ETT 仙人掌 可持久化树套树 数学 洲阁筛 m ...