HDU 6697 Closest Pair of Segments(线段距离)
首先最容易想到的就是N2暴力枚举所有线段去找最小值,但是这样会做了许多无用功。我们可以先对线段排序,使得线段最左侧的端点按照x轴y轴排序,然后我们可以限定在这个线段的矩形框内的所有线段才有可能产生最小值,每次查询对于第i条线段的最近距离,如果第j条线段的最左侧点的x与第i条线段的最右侧点的x差值大于ans,那么可以直接break,之后枚举是没有任何意义的,一定会大于ans,所以加了这部分剪枝复杂度就压缩了很大部分。
// ——By DD_BOND //#include<bits/stdc++.h>
//#include<unordered_map>
//#include<unordered_set>
#include<functional>
#include<algorithm>
#include<iostream>
//#include<ext/rope>
#include<iomanip>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<cstddef>
#include<cstdio>
#include<memory>
#include<vector>
#include<cctype>
#include<string>
#include<cmath>
#include<queue>
#include<deque>
#include<ctime>
#include<stack>
#include<map>
#include<set> #define fi first
#define se second
#define MP make_pair
#define pb push_back #pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; const int MAXN=1e5+;
const double eps=1e-;
const double pi=acos(-1.0);
const ll INF=0x3f3f3f3f3f3f3f3f; inline int dcmp(double x){
if(fabs(x)<eps) return ;
return (x>? : -);
} inline double sqr(double x){ return x*x; } struct Point{
double x,y; int id;
Point(){ x=,y=; }
Point(double _x,double _y):x(_x),y(_y){}
void input(){ scanf("%lf%lf",&x,&y); }
void output(){ printf("%.2f %.2f\n",x,y); }
inline bool operator <(const Point &b)const{
return (dcmp(x-b.x)==? dcmp(y-b.y)< : x<b.x);
}
inline Point operator -(const Point &b)const{
return Point(x-b.x,y-b.y);
}
inline double len2(){ //长度平方
return sqr(x)+sqr(y);
}
inline double len(){ //长度
return sqrt(len2());
}
}; inline double cross(Point a,Point b){ //叉积
return a.x*b.y-a.y*b.x;
} inline double dot(Point a,Point b){ //点积
return a.x*b.x+a.y*b.y;
} inline double dis(Point a,Point b){ //两点的距离
Point p=b-a; return p.len();
} struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e):s(_s),e(_e){} //两点确定直线
void input(){
s.input();
e.input();
}
double length(){
return dis(s,e);
}
}; inline double point_to_line(Point p,Line a){ //点到直线距离
return fabs(cross(p-a.s,a.e-a.s)/a.length());
} inline double point_to_seg(Point p,Line a){ //点到线段距离
if(dcmp(dot(p-a.s,a.e-a.s))<||dcmp(dot(p-a.e,a.s-a.e))<)
return min(dis(p,a.e),dis(p,a.s));
return point_to_line(p,a);
} inline double seg_to_seg(Line u,Line v){
return min( min(point_to_seg(u.s,v),point_to_seg(u.e,v)), min( point_to_seg(v.s,u),point_to_seg(v.e,u)) );
} Line line[MAXN]; bool cmp(Line a,Line b){
return a.s<b.s;
} int main(void){
int T; scanf("%d",&T);
while(T--){
int n; scanf("%d",&n);
for(int i=;i<n;i++){
line[i].input();
if(line[i].e<line[i].s) swap(line[i].s,line[i].e);
}
sort(line,line+n,cmp);
double ans=1e10;
for(int i=;i<n;i++)
for(int j=i+;j<n;j++){
if(dcmp(line[j].s.x-line[i].e.x-ans)>) break;
ans=min(ans,seg_to_seg(line[i],line[j]));
}
printf("%.12f\n",ans);
}
return ;
}
HDU 6697 Closest Pair of Segments(线段距离)的更多相关文章
- HDU 6697 Closest Pair of Segments (计算几何 暴力)
2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description T ...
- 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 ...
- 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 ...
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- 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 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- Qt学习笔记-Widget布局管理
Qt学习笔记4-Widget布局管理 以<C++ GUI Programming with Qt 4, Second Edition>为参考 实例:查找对话框 包含三个文件,f ...
- Git 回滚操作之 revert
https://blog.csdn.net/HobHunter/article/details/79462899 https://blog.csdn.net/secretx/article/detai ...
- 《SaltStack技术入门与实践》—— Event和Reactor系统
Event和Reactor系统 本章节参考<SaltStack技术入门与实践>,感谢该书作者: 刘继伟.沈灿.赵舜东 Event是SaltStack里面的对每个事件的一个记录,它相比job ...
- ES6数据结构Set、Map
一.Set数据结构 Set是无序的不可重复的多个value的集合体,Set结构是类似于数组结构,但是Set中的值都不能重复 常用的属性和方法 size:返回set实例的成员总数 add():添加某个值 ...
- javascript之大文件分段上传、断点续传(一)
需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...
- size_t是什么?
在32位编译器下size_t可看做unsigned int: 在64位编译器下size_t可看做unsigned long long: sizeof返回的数据类型就为size_t. C++之size_ ...
- http协议的深刻理解
https://www.cnblogs.com/mayite/p/9095986.html
- Spring Cloud Commons教程(二)Spring RestTemplate作为负载平衡器客户端
RestTemplate可以自动配置为使用功能区.要创建负载平衡RestTemplate创建RestTemplate @Bean并使用@LoadBalanced限定符. 警告 通过自动配置不再创建Re ...
- Oracle 10046 event
http://czmmiao.iteye.com/blog/1497509 10046事件概述Oracle的10046事件,可以跟踪应用程序所执行的SQL语句,并且得到其解析次数.执行次数,CPU使用 ...
- Crypko 基于滚动条进行的动画是如何实现的?
Crypko 网站里面的下拉滚动条进行的动画感觉非常炫,于是研究了一下她的实现,发现她主要是使用了 ScrollMagic 这个库实现了基于滚动条的动画. 为什么这么确定就是用了 ScrollMagi ...