【题目链接】

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 ; }

最新文章

  1. 如何下载和安装CocoaPods
  2. 用python+selenium获取北上广深成五地PM2.5数据信息并按空气质量排序
  3. centos 7.0安装花生壳
  4. jsp应用
  5. cocos2d-x中使用sqlite
  6. Hdu oj 5522 Numbers 之解题报告
  7. C#基础篇03
  8. c笔试题(1)
  9. git官网和安装使用教程链接
  10. 【Java】java数据库连接中C3P、DBCP、Druid连接池的使用
  11. luogu Eat the Trees
  12. Ng第二课:单变量线性回归(Linear Regression with One Variable)
  13. JNDI 在 J2EE 中的角色
  14. [Bayes ML] This is Bayesian Machine Learning
  15. spring+hibernate 整合异常 Class &#39;org.apache.commons.dbcp.BasicDataSource&#39; not found
  16. kibana做图表无法选取需要选的字段
  17. CSUOJ 1011 Counting Pixels
  18. 报错:405 Method Not Allowed
  19. Jenkins可持续集成
  20. Scala学习笔记(二):运行脚本文件

热门文章

  1. cgroup代码浅析(2)
  2. mysql导出数据到excel的两种方式
  3. cc.Node—Action
  4. mac下用crontab实现pytho3脚本自动定期执行,包括scrapy的定期执行
  5. python3中post请求里带list报错
  6. Python单例模式的实现方式
  7. python+pyqt5实现24点小游戏
  8. java---括号匹配
  9. [bzoj3671][Noi2014][随机数生成器] (贪心+位运算+卡空间)
  10. [luoguP3565] [POI2014]HOT-Hotels(dfs)