【POJ 3714】 Raid
2024-09-08 16:00:50
【题目链接】
http://poj.org/problem?id=3714
【算法】
分治求平面最近点对
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 100010
const double INF = 1e10; int T,i,n; struct info
{
double x,y;
int opt;
} a[MAXN<<]; inline bool cmpx(info a,info b)
{
return a.x != b.x ? a.x < b.x : a.y < b.y;
}
inline bool cmpy(info a,info b)
{
return a.y < b.y;
}
double dist(info a,info b)
{
return (a.opt != b.opt) ? sqrt(abs(a.x - b.x) * abs(a.x - b.x) + abs(a.y - b.y) * abs(a.y - b.y)) : INF;
}
inline double Closest_Pair(int l,int r)
{
int i,j,mid,len = ;
static info s[MAXN];
double d;
if (l == r) return INF;
if (l + == r) return dist(a[l],a[r]);
mid = (l + r) >> ;
d = min(Closest_Pair(l,mid),Closest_Pair(mid+,r));
for (i = l; i <= r; i++)
{
if (abs(a[mid].x - a[i].x) <= d) s[++len] = a[i];
}
sort(s+,s+len+,cmpy);
for (i = ; i <= len; i++)
{
for (j = i + ; j <= len && s[j].y - s[i].y <= d; j++)
{
d = min(d,dist(s[i],s[j]));
}
}
return d;
} int main()
{ scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (i = ; i <= n; i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
a[i].opt = ;
}
for (i = n + ; i <= * n; i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
a[i].opt = ;
}
sort(a+,a+*n+,cmpx);
printf("%.3lf\n",Closest_Pair(,n<<));
} return ; }
最新文章
- 如何下载和安装CocoaPods
- 用python+selenium获取北上广深成五地PM2.5数据信息并按空气质量排序
- centos 7.0安装花生壳
- jsp应用
- cocos2d-x中使用sqlite
- Hdu oj 5522 Numbers 之解题报告
- C#基础篇03
- c笔试题(1)
- git官网和安装使用教程链接
- 【Java】java数据库连接中C3P、DBCP、Druid连接池的使用
- luogu Eat the Trees
- Ng第二课:单变量线性回归(Linear Regression with One Variable)
- JNDI 在 J2EE 中的角色
- [Bayes ML] This is Bayesian Machine Learning
- spring+hibernate 整合异常 Class &#39;org.apache.commons.dbcp.BasicDataSource&#39; not found
- kibana做图表无法选取需要选的字段
- CSUOJ 1011 Counting Pixels
- 报错:405 Method Not Allowed
- Jenkins可持续集成
- Scala学习笔记(二):运行脚本文件
热门文章
- cgroup代码浅析(2)
- mysql导出数据到excel的两种方式
- cc.Node—Action
- mac下用crontab实现pytho3脚本自动定期执行,包括scrapy的定期执行
- python3中post请求里带list报错
- Python单例模式的实现方式
- python+pyqt5实现24点小游戏
- java---括号匹配
- [bzoj3671][Noi2014][随机数生成器] (贪心+位运算+卡空间)
- [luoguP3565] [POI2014]HOT-Hotels(dfs)