【POJ 3714】Raid
【题目链接】:http://poj.org/problem?id=3714
【题意】
给你两类的点;
各n个;
然后让你求出2*n个点中的最近点对的距离;
这里的距离定义为不同类型的点之间的距离;
【题解】
分治法;
不断划分小区间;
求出小区间内的最小值;
为后面的大区间剪枝;
只是要求点的类型不同才能剪枝了;
【Number Of WA】
0
【完整代码】
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100;
const LL oo = 5e18;
struct node{
LL x,y;
int p;
};
int n;
node a[N],b[N];
bool cmp1(node a,node b){
return a.x < b.x;
}
bool cmp2(node a,node b){
return a.y < b.y;
}
LL sqr(LL x){
return x*x;
}
LL dis(node a,node b){
LL temp = 0;
temp+=sqr(a.x-b.x);
temp+=sqr(a.y-b.y);
return temp;
}
LL solve(int l,int r){
LL ret = oo;
if (l>=r) return ret;
if (l+1==r) {
if (a[l].p!=a[r].p)
return dis(a[l],a[r]);
else
return ret;
}
int m = (l+r)>>1;
LL ret1 = solve(l,m),ret2 = solve(m+1,r),temp;
ret = min(ret1,ret2);
int k = 0;
rep2(i,m,l){
temp =sqr(a[m].x-a[i].x);
if (temp>ret && a[m].p != a[i].p) break;
b[++k] = a[i];
}
rep1(i,m+1,r){
temp = sqr(a[m].x-a[i].x);
if (temp>ret && a[m].p != a[i].p) break;
b[++k] = a[i];
}
sort(b+1,b+1+k,cmp2);
rep1(i,1,k){
rep1(j,i+1,k){
if (b[i].p==b[j].p) continue;
temp = sqr(b[i].y-b[j].y);
if (temp > ret) break;
temp = dis(b[i],b[j]);
if (temp < ret) ret = temp;
}
}
return ret;
}
int main(){
//Open();
Close();
int T;
cin >> T;
while (T--){
cin >> n;
rep1(i,1,n){
cin >> a[i].x >> a[i].y;
a[i].p = 0;
}
rep1(i,n+1,2*n){
cin >> a[i].x >> a[i].y;
a[i].p = 1;
}
n<<=1;
sort(a+1,a+1+n,cmp1);
cout << fixed << setprecision(3) << double (sqrt(1.0*solve(1,n))) << endl;
}
return 0;
}
【POJ 3714】Raid的更多相关文章
- 【POJ 3714】 Raid
[题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- 基于 OSGi 的面向服务的组件编程
作者:曹 羽中 (caoyuz@cn.ibm.com), 软件工程师, IBM中国开发中心 出处:http://www.ibm.com/developerworks/cn/opensource/os- ...
- (七)u-boot2013.01.01 for s5pv210:《u-boot启动流程》
转载请注明地址:http://blog.csdn.net/zsy2020314/article/details/9824035 1.关于启动流程 1.1 启动阶段分为3个,bl0,bl1,bl2.下面 ...
- MyEclipse 设置JSP,HTML的默认打开方式,避免出现打开后上面出现浏览器
1. 2. 3. jsp的设置一样,这样myeclipse打开jsp就不会出现上面的浏览器了
- 如何让Jboss的debug在myeclise上运行
1.在windows下运行jboss的debug.bat 看见监听的端口 2.打开myeclipse 点击选择 ①你要配置的名字(随意) ②myeclipse中选中该项目 ③jboss的启动的ip地址 ...
- SpringBoot下支付宝接口的使用
SpringBoot下支付宝接口的使用 前期准备: 参考之前写过的 支付宝接口引入servlet版本 Jar包引入: <!-- 支付宝 --> <dependency> < ...
- java方法名的重载
方法的重载:方法名相同,参数不同,按照参数类型进行匹配 创建一个Simple 类,然后定义了两个方法 package cuteSnow; public class Simple { // 方法的重载, ...
- 在Action中获取servlet API
Struts2的Action组件是不依赖servlet API 的.那么当你在action中的业务需要处理HttpServletRequest和HttpServletResponse的时候(比如要对响 ...
- Springboot2本地锁实践
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交…. 下面我们利用 ...
- 华硕 X201E 拆机
每次笔记本拆机,装好之后.就会发现多了几个螺丝,忘了从哪拧下来了 以下记录下华硕 X201E 清灰拆机过程 1:电脑正面图 2:背面图,一共9个螺丝 3:背面的9个螺丝拧下来,把后盖沿着缝隙扣下来 w ...
- 怎么用命令行运行jar文件
假设你配置好了jre环境,你如今有一个打包好的jar文件,你能够这样子開始运行 java -classpath example.jar mainClass -classpath告诉虚拟机在哪里找类的字 ...