简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage
题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况
分析:点和线段的位置判断可以用叉积判断。给的线段是排好序的,但是点是无序的,所以可以用二分优化。用到了叉积
/************************************************
* Author :Running_Time
* Created Time :2015/10/23 星期五 11:38:18
* File Name :POJ_2318.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 5e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
struct Point { //点的定义
double x, y;
Point (double x=0, double y=0) : x (x), y (y) {}
};
typedef Point Vector; //向量的定义
Point read_point(void) { //点的读入
double x, y;
scanf ("%lf%lf", &x, &y);
return Point (x, y);
}
double polar_angle(Vector A) { //向量极角
return atan2 (A.y, A.x);
}
double dot(Vector A, Vector B) { //向量点积
return A.x * B.x + A.y * B.y;
}
double cross(Vector A, Vector B) { //向量叉积
return A.x * B.y - A.y * B.x;
}
int dcmp(double x) { //三态函数,减少精度问题
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
Vector operator + (Vector A, Vector B) { //向量加法
return Vector (A.x + B.x, A.y + B.y);
}
Vector operator - (Vector A, Vector B) { //向量减法
return Vector (A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p) { //向量乘以标量
return Vector (A.x * p, A.y * p);
}
Vector operator / (Vector A, double p) { //向量除以标量
return Vector (A.x / p, A.y / p);
}
bool operator < (const Point &a, const Point &b) { //点的坐标排序
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool operator == (const Point &a, const Point &b) { //判断同一个点
return dcmp (a.x - b.x) == 0 && dcmp (a.y - b.y) == 0;
}
double length(Vector A) { //向量长度,点积
return sqrt (dot (A, A));
}
double angle(Vector A, Vector B) { //向量转角,逆时针,点积
return acos (dot (A, B) / length (A) / length (B));
}
double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积
return fabs (cross (b - a, c - a)) / 2.0;
}
Vector rotate(Vector A, double rad) { //向量旋转,逆时针
return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad));
}
Vector nomal(Vector A) { //向量的单位法向量
double len = length (A);
return Vector (-A.y / len, A.x / len);
}
Point point_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程
Vector U = p - q;
double t = cross (W, U) / cross (V, W);
return p + V * t;
}
double dis_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式
Vector V1 = b - a, V2 = p - a;
return fabs (cross (V1, V2)) / length (V1);
}
double dis_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式 if (a == b) return length (p - a);
Vector V1 = b - a, V2 = p - a, V3 = p - b;
if (dcmp (dot (V1, V2)) < 0) return length (V2);
else if (dcmp (dot (V1, V3)) > 0) return length (V3);
else return fabs (cross (V1, V2)) / length (V1);
}
Point point_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式
Vector V = b - a;
return a + V * (dot (V, p - a) / dot (V, V));
}
bool inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式
double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1),
c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1);
return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0;
}
bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式
return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0;
}
double area_poly(Point *p, int n) { //多边形面积
double ret = 0;
for (int i=1; i<n-1; ++i) {
ret += fabs (cross (p[i] - p[0], p[i+1] - p[0]));
}
return ret / 2;
}
struct Line {
Point s, e;
Line () {}
Line (Point s, Point e) : s (s), e (e) {}
};
Line board[N];
Point toy[N];
Point b, e;
int ans[N]; int main(void) {
int n, m;
bool fir = true;
while(scanf ("%d", &n) == 1) {
if (!n) break;
if (fir) fir = false;
else puts ("");
scanf ("%d", &m);
b = read_point ();
e = read_point ();
double U, L;
for (int i=0; i<n; ++i) {
scanf ("%lf%lf", &U, &L);
board[i] = Line (Point (U, b.y), Point (L, e.y));
}
board[n] = Line (Point (e.x, b.y), Point (e.x, e.y));
for (int i=0; i<m; ++i) {
toy[i] = read_point ();
}
memset (ans, 0, sizeof (ans));
for (int i=0; i<m; ++i) {
if (toy[i].x < b.x || toy[i].x > e.x || toy[i].y > b.y || toy[i].y < e.y) continue;
int l = 0, r = n, mid, tmp = 0;
while (l <= r) {
mid = (l + r) >> 1;
if (cross (board[mid].s - toy[i], board[mid].e - toy[i]) < 0) {
tmp = mid;
r = mid - 1;
}
else l = mid + 1;
}
ans[tmp]++;
}
for (int i=0; i<=n; ++i) {
printf ("%d: %d\n", i, ans[i]);
}
} return 0;
}
题意:POJ 2398 和上面那题没什么区别,回答的问题不同,线段无序先排序。
/************************************************
* Author :Running_Time
* Created Time :2015/10/23 星期五 11:38:18
* File Name :POJ_2398.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
struct Point { //点的定义
double x, y;
Point (double x=0, double y=0) : x (x), y (y) {}
};
typedef Point Vector; //向量的定义
Point read_point(void) { //点的读入
double x, y;
scanf ("%lf%lf", &x, &y);
return Point (x, y);
}
double polar_angle(Vector A) { //向量极角
return atan2 (A.y, A.x);
}
double dot(Vector A, Vector B) { //向量点积
return A.x * B.x + A.y * B.y;
}
double cross(Vector A, Vector B) { //向量叉积
return A.x * B.y - A.y * B.x;
}
int dcmp(double x) { //三态函数,减少精度问题
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
Vector operator + (Vector A, Vector B) { //向量加法
return Vector (A.x + B.x, A.y + B.y);
}
Vector operator - (Vector A, Vector B) { //向量减法
return Vector (A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p) { //向量乘以标量
return Vector (A.x * p, A.y * p);
}
Vector operator / (Vector A, double p) { //向量除以标量
return Vector (A.x / p, A.y / p);
}
bool operator < (const Point &a, const Point &b) { //点的坐标排序
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool operator == (const Point &a, const Point &b) { //判断同一个点
return dcmp (a.x - b.x) == 0 && dcmp (a.y - b.y) == 0;
}
double length(Vector A) { //向量长度,点积
return sqrt (dot (A, A));
}
double angle(Vector A, Vector B) { //向量转角,逆时针,点积
return acos (dot (A, B) / length (A) / length (B));
}
double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积
return fabs (cross (b - a, c - a)) / 2.0;
}
Vector rotate(Vector A, double rad) { //向量旋转,逆时针
return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad));
}
Vector nomal(Vector A) { //向量的单位法向量
double len = length (A);
return Vector (-A.y / len, A.x / len);
}
Point point_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程
Vector U = p - q;
double t = cross (W, U) / cross (V, W);
return p + V * t;
}
double dis_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式
Vector V1 = b - a, V2 = p - a;
return fabs (cross (V1, V2)) / length (V1);
}
double dis_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式 if (a == b) return length (p - a);
Vector V1 = b - a, V2 = p - a, V3 = p - b;
if (dcmp (dot (V1, V2)) < 0) return length (V2);
else if (dcmp (dot (V1, V3)) > 0) return length (V3);
else return fabs (cross (V1, V2)) / length (V1);
}
Point point_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式
Vector V = b - a;
return a + V * (dot (V, p - a) / dot (V, V));
}
bool inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式
double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1),
c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1);
return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0;
}
bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式
return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0;
}
double area_poly(Point *p, int n) { //多边形面积
double ret = 0;
for (int i=1; i<n-1; ++i) {
ret += fabs (cross (p[i] - p[0], p[i+1] - p[0]));
}
return ret / 2;
}
struct Line {
Point s, e;
Line () {}
Line (Point s, Point e) : s (s), e (e) {}
};
bool cmp(Line A, Line B) {
return A.s.x < B.s.x || (A.s.x == B.s.x && A.e.x < B.e.x);
}
Line board[N];
Point toy[N];
Point b, e;
int ans[N]; int main(void) {
int n, m;
while(scanf ("%d", &n) == 1) {
if (!n) break;
scanf ("%d", &m);
b = read_point ();
e = read_point ();
double U, L;
for (int i=0; i<n; ++i) {
scanf ("%lf%lf", &U, &L);
board[i] = Line (Point (U, b.y), Point (L, e.y));
}
board[n] = Line (Point (e.x, b.y), Point (e.x, e.y));
sort (board, board+1+n, cmp);
for (int i=0; i<m; ++i) {
toy[i] = read_point ();
}
memset (ans, 0, sizeof (ans));
for (int i=0; i<m; ++i) {
if (toy[i].x < b.x || toy[i].x > e.x || toy[i].y > b.y || toy[i].y < e.y) continue;
int l = 0, r = n, mid, tmp = 0;
while (l <= r) {
mid = (l + r) >> 1;
if (cross (board[mid].s - toy[i], board[mid].e - toy[i]) < 0) {
tmp = mid;
r = mid - 1;
}
else l = mid + 1;
}
ans[tmp]++;
}
puts ("Box");
for (int i=1; i<=m; ++i) {
int cnt = 0;
for (int j=0; j<=n; ++j) {
if (ans[j] == i) ++cnt;
}
if (cnt) {
printf ("%d: %d\n", i, cnt);
}
}
} return 0;
}
简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage的更多相关文章
- 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage
POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...
- POJ 2318 TOYS && POJ 2398 Toy Storage(几何)
2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...
- POJ 2318 TOYS/POJ 2398 Toy Storage
计算几何终于开坑了... 叉积+二分. #include<iostream> #include<cstdio> #include<cstring> #include ...
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- poj 2398 Toy Storage(计算几何)
题目传送门:poj 2398 Toy Storage 题目大意:一个长方形的箱子,里面有一些隔板,每一个隔板都可以纵切这个箱子.隔板将这个箱子分成了一些隔间.向其中扔一些玩具,每个玩具有一个坐标,求有 ...
- 几何+点与线段的位置关系+二分(POJ2318)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10666 Accepted: 5128 Description ...
- POJ 2398 - Toy Storage 点与直线位置关系
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5439 Accepted: 3234 Descr ...
- 2018.07.04 POJ 2398 Toy Storage(二分+简单计算几何)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad have a problem: their ch ...
- POJ 2318 TOYS (叉乘判断)
<题目链接> 题目大意: 给出矩形4个点和n个挡板俩顶点的位置,这n个挡板将该矩形分成 n+1块区域,再给你m个点的坐标,然你输出每个区域内有几个点. 解题思路: 用叉乘即可简单判断点与直 ...
随机推荐
- hibernate操作数据库例子
1.工程目录结构如下 2.引入需要的jar包,如上图. 3.创建持久化类User对应数据库中的user表 package com.hibernate.配置文件.pojo; import java.sq ...
- SQL面试积累
以下题目都在MySQL上测试可行,有疏漏或有更优化的解决方法的话欢迎大家提出,我会持续更新的:) 有三个表,如果学生缺考,那么在成绩表中就不存在这个学生的这门课程成绩的记录,写一段SQL语句,检索出每 ...
- DCMTK3.6.0 (MT支持库)安装 完整说明
环境WIN7 + VisualStudio2010 + dcmtk3.6.0 + Cmake2.8.6 准备工作: 从dcmtk官方网站下载源代码及支持库文件.分别名为:dcmtk-3.6.0 dcm ...
- 发个题目坑 二模03day1
1.数列(seq2.pas/c/cpp) 题目描述 一个数列定义如下:f(1) = 1,f(2) = 1,f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.给定 A ...
- 在 FREEBUF 投放广告
在 FREEBUF 投放广告 FreebuF黑客与极客—高质量的全球互联网安全媒体,同时也是爱好者们交流.分享安全技术的最佳平台.本站读者群以IT.政企信息安全人员.互联网安全爱好者和学生为主,对互联 ...
- 【转】利用optimize、存储过程和系统表对mysql数据库表进行批量碎片清理释放表空间
本文收集于本人的笔记本,由于找不到原文出处.在此省略,如哪位知道可以联系我加上. 核心是利用mysql系统表和“optimize table 表名”命令,对mysql数据表进行空间的释放.由于dele ...
- 如何用BMFont制作图片字
1: 运行程序,单击鼠标左键点亮相应位置的字母,比如:0.1.2./ 等2: 选择 Edit->Open Image Manager.弹出一个“Image Manager" 对话框3: ...
- Ubuntu 13.04安装搜狗输入法
Ubuntu 13.04安装搜狗输入法 [日期:2013-07-08] 来源:Linux公社 作者:LinuxIDC.com [字体:大 中 小] 目标:在Ubuntu 13.04以及基于U ...
- css3学习总结3--CSS3图像边框
border-image属性 .className{ border-image:url(/course/54d1cae088dba03f2cd1fec1/img/border.png) 20 20 2 ...
- Replace Nested Conditional with Guard Clauses(用卫语句代替嵌套循环)
函数中的条件逻辑,使人难以看清正常的执行路径. 使用卫语句表现所有特殊情况. double getPayAmount() {double result;if (_isDead) result = de ...