【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][ ...
随机推荐
- Vue学习之路第六篇:v-on
v-on指令用来触发页面事件的指令. <body> <div id="app"> <button v-on:click="show()&qu ...
- Shell(四)函数
函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. 一.格式 shell中函数的定义格式如下: [ function ] funname [()] { action; ...
- spring mvc 下载的时候中文文件名不显示
Headers.add("Content-Disposition", "attachment;filename=" + new String(file.getB ...
- VUE:模板语法(小白自学)
VUE:模板语法 一:何为声明式 安装规定的语法,去实现一些效果(不需要管流程). 二:模板语法 <!DOCTYPE html> <html> <head> < ...
- nutch如何修改regex-urlfilter.txt爬取符合条件的链接
例如我在爬取学生在线的时候,发现爬取不到特定的通知,例如<中粮福临门助学基金申请公告>,通过分析发现原来通知的链接被过滤掉了,下面对过滤url的配置文件regex-urlfilter.tx ...
- PatentTips - Increasing turbo mode residency of a processor
BACKGROUND Many modern operating systems (OS's) use the Advanced Configuration and Power Interface ( ...
- AWS使用心得:当初我曾错过的那些宝贵经验
在今天的文章中,我整理出了大量当初以前错过.而至今仍将我追悔莫及的Amazon Web Services(简称AWS)使用心得. 在几年来的实践其中,我通过在AWS之上新手构建及部署各类应用程序而积累 ...
- python解压,压缩,以及存数据库的相关操作
zipfile实现压缩整个目录和子目录 import os,shutil,zipfile,glob def dfs_get_zip_file(input_path,result): # files = ...
- Default Document <defaultDocument> IIS中的默认页面
https://docs.microsoft.com/en-us/iis/configuration/system.webserver/defaultdocument/index Default do ...
- 文档相关命令-cat命令查看一个文件
用于查看一个文件的内容并将其显示在屏幕上 cat 后直接加上文件名 -n 表示显示行号 cat -n dirb/filee -A 显示所有内容包括特殊字符 cat -A dirb/filee