POJ-2318 TOYS 计算几何 判断点在线段的位置
题目链接:https://cn.vjudge.net/problem/POJ-2318
题意
在一个矩形内,给出n-1条线段,把矩形分成n快四边形
问某些点在那个四边形内
思路
二分+判断点与位置关系
提交过程
| WA*n | x1和x2,y1和y2在复制的时候没分清(哭 |
| WA | 可能存在二分问题? |
| AC |
代码
#define PI 3.1415926
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const double eps=1e-10;
const int maxn=5e3+20;
struct Point{
double x, y;
Point(int x=0, int y=0):x(x), y(y) {}
// no known conversion for argument 1 from 'Point' to 'Point&'
Point operator + (Point p){return Point(x+p.x, y+p.y);}
Point operator - (Point p){return Point(x-p.x, y-p.y);}
Point operator * (double k){return Point(k*x, k*y);}
Point operator / (double k){return Point(x/k, y/k);}
bool operator < (Point p) const{return (x==p.x)?(y<p.y):(x<p.x);} // need eps?
bool operator == (const Point p) const{return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;}
double norm(void){return x*x+y*y;}
double abs(void){return sqrt(norm());}
double dot(Point p){return x*p.x+y*p.y;} // cos
double cross(Point p){return x*p.y-y*p.x;} // sin
};
struct Segment{Point p1, p2;};
struct Circle{Point o; double rad;};
typedef Point Vector;
typedef vector<Point> Polygon;
typedef Segment Line;
int ccw(Point p0, Point p1, Point p2){
Vector v1=p1-p0, v2=p2-p0;
if (v1.cross(v2)>eps) return 1; // anti-clockwise
if (v1.cross(v2)<-eps) return -1; // clockwise
if (v1.dot(v2)<0) return 2;
if (v1.norm()<v2.norm()) return -2;
return 0;
}
Point project(Segment s, Point p){
Vector base=s.p2-s.p1;
double k=(p-s.p1).cross(base)/base.norm();
return s.p1+base*k;
}
Point reflect(Segment s, Point &p){
return p+(project(s, p)-p)*2;
}
double lineDist(Line l, Point p){
return abs((l.p2-l.p1).cross(p-l.p1)/(l.p2-l.p1).abs());
}
double SegDist(Segment s, Point p){
if ((s.p2-s.p1).dot(p-s.p1)<0) return Point(p-s.p1).abs();
if ((s.p1-s.p2).dot(p-s.p2)<0) return Point(p-s.p2).abs();
return abs((s.p2-s.p1).cross(p-s.p1)/(s.p2-s.p1).abs());
}
bool intersect(Point p1, Point p2, Point p3, Point p4){
return ccw(p1, p2, p3)*ccw(p1, p2, p4)<=0 &&
ccw(p3, p4, p1)*ccw(p3, p4, p2)<=0;
}
Point getCrossPoint(Segment s1, Segment s2){
Vector base=s2.p2-s2.p1;
double d1=abs(base.cross(s1.p1-s2.p1));
double d2=abs(base.cross(s1.p2-s2.p1));
double t=d1/(d1+d2);
return s1.p1+(s1.p2-s1.p1)*t;
}
double area(Polygon poly){
double res=0; long long size=poly.size();
for (int i=0; i<poly.size(); i++)
res+=poly[i].cross(poly[(i+1)%size]);
return abs(res/2);
}
int contain(Polygon poly, Point p){
int n=poly.size();
bool flg=false;
for (int i=0; i<n; i++){
Point a=poly[i]-p, b=poly[(i+1)%n]-p;
if (ccw(poly[i], poly[(i+1)%n], p)==0) return 1; // 1 means on the polygon.
if (a.y>b.y) swap(a, b);
if (a.y<0 && b.y>0 && a.cross(b)>0) flg=!flg;
}return flg?2:0; // 2 fo inner, 0 for outer.
}
Polygon convexHull(Polygon poly){
if (poly.size()<3) return poly;
Polygon upper, lower;
sort(poly.begin(), poly.end());
upper.push_back(poly[0]); upper.push_back(poly[1]);
lower.push_back(poly[poly.size()-1]); lower.push_back(poly[poly.size()-2]);
for (int i=2; i<poly.size(); i++){
for (int n=upper.size()-1; n>=1 && ccw(upper[n-1], upper[n], poly[i])!=-1; n--)
upper.pop_back();
upper.push_back(poly[i]);
}
for (int i=poly.size()-3; i>=0; i--){
for (int n=lower.size()-1; n>=1 && ccw(lower[n-1], lower[n], poly[i])!=-1; n--)
lower.pop_back();
lower.push_back(poly[i]);
}
for (int i=1; i<lower.size(); i++)
upper.push_back(lower[i]);
return upper;
}
Segment seg[maxn];
int n, m;
int solve(Point p){
int l=0, r=n;
while (l<r){
int mid=l+(r-l)/2;
if (ccw(seg[mid].p1, seg[mid].p2, p)==-1) r=mid;
else l=mid+1;
}
for (int i=max(l-3, 0); i<=min(l+3, n); i++)
if (ccw(seg[i].p1, seg[i].p2, p)==-1)
return l;
}
int main(void){
long long x, y, x1, y1, x2, y2, xt1, xt2;
while (scanf("%d", &n)==1 && n){
int bin[maxn]={0};
scanf("%d%lld%lld%lld%lld", &m, &x1, &y1, &x2, &y2);
for (int i=0; i<n; i++){
scanf("%lld%lld", &xt1, &xt2);
seg[i].p1=Point(xt1-x1, y1-y2);
seg[i].p2=Point(xt2-x1, y2-y2);
}
seg[n].p1=Point(x2-x1, y1-y2);
seg[n].p2=Point(x2-x1, y2-y2);
while (m--){
scanf("%lld%lld", &x, &y);
bin[solve(Point(x-x1, y-y2))]++;
}
for (int i=0; i<=n; i++)
printf("%d: %d\n", i, bin[i]);
printf("\n");
}
return 0;
}
| Time | Memory | Length | Lang | Submitted |
|---|---|---|---|---|
| 204ms | 716kB | 4134 | G++ | 2018-08-01 12:19:23 |
POJ-2318 TOYS 计算几何 判断点在线段的位置的更多相关文章
- poj 2318 TOYS(计算几何 点与线段的关系)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12015 Accepted: 5792 Description ...
- POJ 2318 TOYS | 二分+判断点在多边形内
题意: 给一个矩形的区域(左上角为(x1,y1) 右下角为(x2,y2)),给出n对(u,v)表示(u,y1) 和 (v,y2)构成线段将矩形切割 这样构成了n+1个多边形,再给出m个点,问每个多边形 ...
- POJ 2318 TOYS(计算几何)
跨产品的利用率推断点线段向左或向右,然后你可以2分钟 代码: #include <cstdio> #include <cstring> #include <algorit ...
- 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage
题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- POJ 2318 TOYS (计算几何,叉积判断)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8661 Accepted: 4114 Description ...
- TOYS - POJ 2318(计算几何,叉积判断)
题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数. 分析:做的第一道计算几何题目....使用叉积判断方 ...
- 【POJ】2318 TOYS ——计算几何+二分
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10281 Accepted: 4924 Description ...
- POJ 2318 TOYS (叉乘判断)
<题目链接> 题目大意: 给出矩形4个点和n个挡板俩顶点的位置,这n个挡板将该矩形分成 n+1块区域,再给你m个点的坐标,然你输出每个区域内有几个点. 解题思路: 用叉乘即可简单判断点与直 ...
随机推荐
- Win7下安装Flash低版本
我把HKEY_LOCAL_MACHINE\SOFTWARE\Macromedia\FlashPlayer\SafeVersions中高于要装的版本的项目都删了,还是不行. 看了这个帖子后发现,原来64 ...
- CentOS 7.5安装pycharm
环境 安装环境: windows 10 Pro CentOS Linux release 7.6.1810 (Core) VMWare Workstation 15 Pro 安装图形化界面包 首先更新 ...
- 注解实战Beforeclass和Afterclass
package com.course.testng;import org.testng.annotations.*; public class BasicAnnotation { //最基本的注解,用 ...
- HDU 2048 神、上帝以及老天爷( 错排 )
链接:传送门 思路:错排模板,典型错排问题,n个人所有人都不会抽到自己的方案数为 Dn = (n-1) * (Dn-1 + Dn-2) /******************************* ...
- [USACO 2009 Feb Gold] Fair Shuttle (贪心+优先队列)
题目大意:有N个站点的轻轨站,有一个容量为C的列车起点在1号站点,终点在N号站点,有K组牛群,每组数量为Mi(1≤Mi≤N),行程起点和终点分别为Si和Ei(1≤Si<Ei≤N).计算最多有多少 ...
- Linux 网络属性管理
Linux网络基础管理-1:IPv4 地址分类: 点分十进制:0.0.0.0-255.255.255.255 A类: 0 0000000 - 0 1111111: 1-127 网络数:126, 1 ...
- db2 -- 存储过程01
接下来项目在技能可能偏向数据库方面,补习下. 学习写第一个db2在存储过程,记录下. ---- stored procedures code CREATE OR REPLACE PROCEDURE & ...
- 中山纪念中学培训杂题(难的都不在这里面qwq)
来中山纪中半个月了,差不多就要结束了, 写一些之前考试能更正的题解吧,还有一些不是给人做的(比如IOI2018互测.. 备注:我不会的就没有放上来了,所有数学有关的基本上都死了. 所以这里的题目都是相 ...
- snprintf
snprintf(),函数原型为int snprintf(char *str, size_t size, const char *format, ...). 将可变参数 “…” 按照format的 ...
- tring.Format格式化用法
(数字保留两位小数,且每隔3为用逗号隔开): string.format("1f,.2d",333) -->333.00 string.format("1f,.2d ...