【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][ ...
随机推荐
- 四、服务器上的 Git
一个远程仓库通常只是一个裸仓库(bare repository)— 即一个没有当前工作目录的仓库.因为该仓库仅仅作为合作媒介,不需要从磁碟检查快照:存放的只有 Git 的资料.简单的说,裸仓库就是你专 ...
- MyBatis学习总结(19)——Mybatis传多个参数(三种解决方案)
据我目前接触到的传多个参数的方案有三种. 第一种方案 DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xm ...
- 数据结构实现(四)二叉查找树java实现
转载 http://www.cnblogs.com/CherishFX/p/4625382.html 二叉查找树的定义: 二叉查找树或者是一颗空树,或者是一颗具有以下特性的非空二叉树: 1. 若左子树 ...
- 在CentOS6.5下配置安装LVS
先上一个图,在内网测试机上安装了一个虚拟机,已经安装好了CentOS 乖乖连wget都没有安装,先安装wget吧 sudo yum install wget
- C#-基础部分思维导图
C#-基础部分思维导图 链接:http://pan.baidu.com/s/1jHNgS78 密码:3i74 如果有错我,请告诉我,传播错误的知识是违法的.
- 鸟哥的Linux私房菜-----11、压缩指令与正则表示法
- 海尔公布首套智能套装SmartCare,主打智能、简单
2014CIE智能硬件首发会于11月6-8日在上海国际展览中心举行.吸引了海尔.英特尔.百度等知名企业的积极參与. 作为国内智能家居领域的先驱者,海尔在本次大会上首发了轻量智能套装SmartC ...
- Test Precisely and Concretely
Test Precisely and Concretely Kevlin Henney IT IS IMPORTANT TO TEST for the desired, essential behav ...
- activity生命周期的onPause和onStop
搞了这么长时间的android开发,却对一些基础的东西一直模棱两可...就比方这个onPause和onStop. 假设从一个界面,跳到还有一个界面,那么是调用哪个呢? 经过我的实验.搞清楚了.onPa ...
- 计算机网络 4.网络层与IP协议
网络中的每一台主机和路由器都有一个网络层部分.而路由器中也没有网络层以上的层次.网络层是协议栈中最复杂的层次. 转发forwarding:当一个分组到达某路由器的输入链路时.该路由器将分组移动到适当的 ...