【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][ ...
随机推荐
- IOS - autoresizingMask
提醒:当frame设定死,慎用autoresizingMask:否则该frame变形的难以想象.
- vue2 在methods 中无法获取this对象
在methods中使用箭头函数无法获取this ExamName:()=> { console.log(this);} 这样就行了: ExamName:function() { console. ...
- event 下鼠标坐标的获取
event.clientX.event.clientY 鼠标相对于浏览器窗口可视区域的X,Y坐标(窗口坐标),可视区域不包括工具栏和滚动条.IE事件和标准事件都定义了这2个属性 event.pageX ...
- UGUI图集管理
using UnityEngine; using System.Collections; using System.Collections.Generic; //纹理图集加载管理 public cla ...
- Java 多线程均匀处理同一个List中的数据
需求:使用多线程来处理同一个List中的数据,希望每个线程处理的数量是均匀的 事例代码如下: public class Test { static class HandleThread extends ...
- Oracle安装和使用说明
很久没有安装Oracle了,今天试了一下重新安装Oracle,然后做了一个总结: 一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要 ...
- Linux进程的内存布局
这张图很好,注意其中最上面是高位地址,虽然很多个0,但是c开头的,不要看反了: 更具体的可以看这里: A.正文段.这是由cpu执行的机器指令部分.通常,正文段是可共享的,所以即使是经常执行的程序(如文 ...
- stl之hash_map
- scikit-learn:3.5. Validation curves: plotting scores to evaluate models
參考:http://scikit-learn.org/stable/modules/learning_curve.html estimator's generalization error can b ...
- WebView简介(加速加载篇)
从Android 3.0开始,Android的2D渲染管线可以更好的支持硬件加速.硬件加速使用GPU进行View上的绘制操作. 硬件加速可以在一下四个级别开启或关闭: Application Acti ...