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个区域内.问落在每个区域的个数. 分析:二分查找落在哪个区域内.叉积判断点与线 ...
随机推荐
- 《学习OpenCV》课后习题解答5
题目:(P104) 为一个图像创建多个图像头.读取一个大小至少为100*100的图像.另创建两个图像头并设置它们的origion,depth,nChannels和widthStep属性同之前读取的图像 ...
- windows批处理学习(字符换操作)---04
转自:https://www.cnblogs.com/DswCnblog/p/5432326.html 1.截取字符串 截取字符串可以说是字符串处理功能中最常用的一个子功能了,能够实现截取字符串中的特 ...
- AWVS使用基础教程
什么是AWVS Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网络爬虫测试你的网站安全,检测流行安全漏洞,现已更新到10.(下 ...
- Runtime之IMP指针,isa指针
要了解 isa 指针先了解下类的定义在xcode中用快捷键Shift+Cmd+O 搜索objc.h 能看到类的定义:了解 Paste_Image.png 可以看出:objc_object:Object ...
- HUAS 1477 经营与开发(贪心)
考虑DP,令dp[i][j][k]当前在第i个星球,用了j次维修,k次开采后所获得的最大价值.复杂度为O(n^3).超时 如果我们发现,对于初始时能力值为w所能产生的最大价值y,初始时能力值为1所能产 ...
- 51nod 1385凑数字(字符串+构造)
题目大意: 给定一个n,要求找出一个最短的字符串S,使得所有1到n的整数都是S的子序列. 比如n=10,那么S=”1234056789”的时候,是满足条件的.这个时候S的长度是10. 现在给出一个n, ...
- BZOJ4753:[JSOI2016]最佳团体——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4753 JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号. ...
- 你会喜欢的前端^o^!
前端那些事儿 网页设计常用色彩搭配表 很漂亮的alert弹出框 一个让你想到即可做到的web弹窗/层解决方案 基于HTML5的在绘图特效平台(酷炫)
- 表单验证:nice Validator
nice Validator使用文档:http://niceue.com/validator/ 一.自定义验证规则: //大类表单新增修改验证提交 $("#addbigCategory&qu ...
- CentOS7搭建 Hadoop + HBase + Zookeeper集群
摘要: 本文主要介绍搭建Hadoop.HBase.Zookeeper集群环境的搭建 一.基础环境准备 1.下载安装包(均使用当前最新的稳定版本,截止至2017年05月24日) 1)jdk-8u131 ...