HDU 6697 Closest Pair of Segments (计算几何 暴力)
2019 杭电多校 10 1007
题目链接:HDU 6697
比赛链接:2019 Multi-University Training Contest 10
Problem Description
The closest pair of points problem is a well-known problem of computational geometry. In this problem, you are given \(n\) points in the Euclidean plane and you need to find a pair of points with the smallest distance between them.
Now, Claris, the brilliant one who has participated in programming contests for several years, is trying to solve a harder problem named the closest pair of segments problem, which also has a quite simple description as above.
However, the problem seems even too hard for Claris and she is asking you for help.
Now \(n\) segments are lying on the Euclidean plane, you are asked to pick two different segments and then pick a point on the two segments respectively to minimize the distance between these two points.
For simplicity, any two given segments share no common point, and you don't need to show her the two chosen points, but the distance between them instead.
Input
The input contains several test cases, and the first line contains a single integer \(T (1\le T\le 200)\), the number of test cases.
For each test case, the first line contains one integer \(n (2\le n\le 10000)\), which is the number of segments on the Euclidean plane.
The following \(n\) lines describe all the segments lying on the Euclidean plane, the \(i\)-th of which contains for integers \(x_1,y_1,x_2\) and \(y_2\) describing a segment that connects \((x_1,y_1)\) and \((x_2,y_2)\), where \(−10^9\le x_1,y_1,x_2,y_2\le 10^9\).
It's guaranteed that the two endpoints of each segment do not coincide, any two given segments do not intersect with each other in each test case, and no more than \(20\) test cases satisfy \(n>1000\).
Output
For each test case, output a line containing a single real number for the answer to the closest pair of segments problem with an absolute or relative error of at most \(10^{−6}\).
Precisely speaking, assume that your answer is \(a\) and and the jury's answer is \(b\), your answer will be considered correct if and only if \(\frac{|a−b|}{max\{1,|b|\}}\le 10^{−6}\).
Sample Input
2
2
0 1 1 2
1 1 2 0
2
0 1 1 2
2 2 3 1
Sample Output
0.707106781187
1.000000000000
Solution
题意
类似于计算几何中的最近点对问题,本题求的是最近线段对。
给定 \(n\) 条线段,求出最近线段对之间的距离。
题解
暴力 剪枝
比赛时我用了三角剖分,结果超时了。
赛后补题时看到了这篇博客:HDU 6697 Closest Pair of Segments(线段距离)
原来暴力加上剪枝就能过。思路是这样的:
首先将线段的左侧端点按照横坐标为第一关键字,纵坐标为第二关键字排序。然后暴力找所有线段对,维护最小值 \(ans\)。如果当前查询的线段对中,右侧线段的左端点与左侧线段的右端点的横坐标差值大于 \(ans\) 时,就不用再找更右侧的直线了。这样剪枝能大大减少时间复杂度。
时限给了 20s,大概 1.3s 就能跑完。
题解看不懂
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e5 + 10;
inline int dcmp(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
}
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
void input() {
scanf("%lf%lf", &x, &y);
}
bool operator<(const Point &a) const {
return (!dcmp(x - a.x))? dcmp(y - a.y) < 0: x < a.x;
}
bool operator==(const Point &a) const {
return dcmp(x - a.x) == 0 && dcmp(y - a.y) == 0;
}
db dis2(const Point a) {
return pow(x - a.x, 2) + pow(y - a.y, 2);
}
db dis(const Point a) {
return sqrt(dis2(a));
}
db dis2() {
return x * x + y * y;
}
db dis() {
return sqrt(dis2());
}
Point operator+(const Point a) {
return Point(x + a.x, y + a.y);
}
Point operator-(const Point a) {
return Point(x - a.x, y - a.y);
}
db dot(const Point a) {
return x * a.x + y * a.y;
}
db cross(const Point a) {
return x * a.y - y * a.x;
}
};
typedef Point Vector;
struct Line {
Point s, e;
Line() {}
Line(Point s, Point e) : s(s), e(e) {}
void input() {
s.input();
e.input();
}
db length() {
return s.dis(e);
}
// 点到直线的距离
db point_to_line(Point p) {
return fabs((p - s).cross(e - s) / length());
}
// 点到线段的距离
db point_to_seg(Point p) {
if(dcmp((p - s).dot((e - s))) < 0 || dcmp((p - e).dot((s - e))) < 0) {
return min(p.dis(s), p.dis(e));
}
return point_to_line(p);
}
// 线段到线段的距离
db seg_to_seg(Line l) {
return min(min(point_to_seg(l.s), point_to_seg(l.e)), min(l.point_to_seg(s), l.point_to_seg(e)));
}
};
Line l[maxn];
int cmp(Line l1, Line l2) {
return l1.s < l2.s;
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
l[i].input();
if(l[i].e < l[i].s) swap(l[i].s, l[i].e);
}
sort(l, l + n, cmp);
double ans = 1e10;
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
// 剪枝部分
if(dcmp((l[j].s.x - l[i].e.x) - ans) > 0) {
break;
}
ans = min(ans, l[i].seg_to_seg(l[j])); // 更新最小值
}
}
printf("%.12lf\n", ans);
}
return 0;
}
HDU 6697 Closest Pair of Segments (计算几何 暴力)的更多相关文章
- HDU 6697 Closest Pair of Segments(线段距离)
首先最容易想到的就是N2暴力枚举所有线段去找最小值,但是这样会做了许多无用功.我们可以先对线段排序,使得线段最左侧的端点按照x轴y轴排序,然后我们可以限定在这个线段的矩形框内的所有线段才有可能产生最小 ...
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- 2.11 2D平面最近点对问题[closest pair problem]
[本文链接] http://www.cnblogs.com/hellogiser/p/closest-pair-problem.html [题目] 给定平面上N个点的坐标,找出距离最近的两个点之间的距 ...
- HDU 5877 Weak Pair(弱点对)
HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Jav ...
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- Codeforces Round #185 (Div. 2) C. The Closest Pair 构造
C. The Closest Pair Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/p ...
- HDU 5572 An Easy Physics Problem (计算几何+对称点模板)
HDU 5572 An Easy Physics Problem (计算几何) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5572 Descripti ...
- HDU 3267 Graph Game(博弈论+图论+暴力)
题面传送门 题意: 有一棵 \(n\) 个节点的图 \(G\),R 和 B 两个人轮流操作,R 先操作. 每次操作 R 可以染红任意一条未染色的边,B 可以染蓝任意一条未染色的边 R 的目标是染成一棵 ...
随机推荐
- IE8 indexOf
因为ie8中的js数组没有indexOf方法,所以使用之前要先加入这段js代码 if (!Array.prototype.indexOf) { Array.prototype.indexOf = fu ...
- HTML5: HTML5 Audio(音频)
ylbtech-HTML5: HTML5 Audio(音频) 1.返回顶部 1. HTML5 Audio(音频) HTML5 提供了播放音频文件的标准. 互联网上的音频 直到现在,仍然不存在一项旨在网 ...
- VIP视频下载终结器
youtube-dl: Youtube-dl是谷歌github上的一个开源项目,它是一款轻量级的命令行 下载实用工具,阿刚曾在乐软博客里文章<不仅仅是youtube,youtube-dl在线视频 ...
- 点读系列《jmeter官方用户手册》
官网:http://jmeter.apache.org/usermanual/ 说明:十八元件.十九属性.二十函数,涉及清单内容暂未仔细阅读,个人觉得一是仅供使用参考,二是适合单独写文章来解读 一.让 ...
- 转 Jmeter如何把响应数据的结果保存到本地的一个文件
当做性能压测时,可能会需要把响应数据的一些字段统计出来.这里简单介绍一下. 1.首先把接口调通,确定需要统计的字段,这里以统计ccmpSeq字段来做例子. 2.添加正则表达式提取器,用来提取响应结果中 ...
- Spring Cloud Alibaba
Spring Cloud Alibaba Dubbo Dubbo Dubbo 系列 [Dubbo 系列总结] [Dubbo 系列(01)最简使用姿态] [Dubbo 系列(02)整体架构] Dubbo ...
- Eureka 系列(02)Eureka 一致性协议
目录 Eureka 系列(02)Eureka 一致性协议 0. Spring Cloud 系列目录 - Eureka 篇 1. 服务发现方案对比 1.1 技术选型 1.2 数据模型 2. Eureka ...
- git 处于游离的状态的解决办法
在idea下将代码回退到某一历史版本,修改后push提醒detaced head,即处于游离状态,使用 git branch命令(辅助git status查看提交状态)查看: 在git bash下切换 ...
- Zookeeper 集群的安装及高可用性验证已完成!
安装包 kafka_2.12-0.10.2.0.tgz zookeeper-3.3.5.tar.gz Java 环境 Zookeeper 和 Kafka 的运行都需要 Java 环境,Kafka 默认 ...
- Selenium3 + Python3自动化测试系列十二——窗口截图与关闭浏览器
窗口截图 自动化用例是由程序去执行的,因此有时候打印的错误信息并不十分明确.如果在脚本执行出错的时候能对当前窗口截图保存,那么通过图片就可以非常直观地看出出错的原因.WebDriver提供了截图函数g ...