hdu 1007 最近点对问题(Splay解法)
为什么要写这个题、、经典啊,当然,别以为我用分治做的,不过主要思想还是那神奇的六个点共存(一个h*2h的矩形中最多能放下多少个点使得两两距离不超过h)
其实我是在这里看到的
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lineSweep
排个序,然后扫描过去,每次确定Y的范围,暴力找每个点(其实这是O(1)的)
蛮不错的哦。
写完后发现比分治写的快了好多啊,估计是我不会写分治吧T_T,总之,现在跑到了rank 4了。。。
数据结构就是这样的直接啊!
/* **********************************************
Author : wuyiqi
Created Time: 2013-8-24 12:54:35
File Name : ruocai.cpp
*********************************************** */
#include <cstdio>
#include <cmath>
#include <stack>
#include <algorithm>
using std::stack;
using std::pair;
using std::make_pair;
#define L x->c[0]
#define R x->c[1]
#define KT root->c[1]->c[0]
const int maxn = 200010;
struct node{
node *c[2] , *fa;
int id,sz;
double val,x;
inline bool d() {
return fa->c[0] == this;
}
inline void setc(int d,node *s) {
c[d] = s;
s->fa = this;
}
inline void up() {
sz = c[0]->sz + c[1]->sz + 1;
}
}NODE[maxn],*null=&NODE[0];
node *ID[maxn];
int top;
struct Splay_tree{
node *root;
void Rotate(node *x,int f){
node *y = x->fa;
y->setc(!f,x->c[f]);
x->fa = y->fa;
if(y->fa != null) y->fa->setc(!y->d(),x);
x->setc(f,y);
y->up();
}
void Splay(node *x,node *goal) {
while(x->fa!=goal) {
if(x->fa->fa == goal) Rotate(x,x->d());
else {
int f = x->fa->d();
x->d() == f ? Rotate(x->fa,f) : Rotate(x,!f);
Rotate(x,f);
}
}
x->up();
if(goal == null) {
root = x;
}
}
void RTO(int k,node *goal) {
node *x = root;
while(L->sz + 1 != k) {
if(k < L->sz + 1) x = L;
else {
k -= L->sz + 1;
x = R;
}
}
Splay(x,goal);
}
void insert(node* &x,node *y) {
if(x == null) {
x = y;
return ;
}
if(y->val <= x->val) {
insert(x->c[0],y);
x->c[0]->fa = x;
} else {
insert(x->c[1],y);
x->c[1]->fa = x;
}
x->up();
}
node* new_node(int id,double x,double y) {
node* tmp = &NODE[++top];
tmp->fa = null;
tmp->val = y;
tmp->x = x;
tmp->sz = 1;
tmp->id = top;
tmp->c[0] = tmp->c[1] = null;
ID[id] = tmp;
return tmp;
}
void insert(int id,double x,double y) {
node *tmp = new_node(id,x,y);
insert(root,tmp);
}
void init() {
root = null;
}
void Del_root() {
node *t = root;
if(t->c[1] != null) {
root = t->c[1];
RTO(1,null);
root->c[0] = t->c[0];
if(root->c[0] != null)
root->c[0]->fa = root;
} else {
root = root->c[0];
}
root->fa = null;
if(root != null) root->up();
} void Del(node *tmp) {
Splay(tmp,null);
Del_root();
}
node* find_pre(node *x,double v) {
if(x == null) return null;
if(x->val < v) {
node *tmp = find_pre(x->c[1],v);
return tmp == null ? x : tmp;
}
else {
return find_pre(x->c[0],v);
}
}
node* find_succ(node *x,double v) {
if(x == null) return null;
if(x->val > v ) {
node *tmp = find_succ(x->c[0],v);
return tmp == null ? x : tmp;
} else {
return find_succ(x->c[1],v);
}
}
void search(double a,double b,double &ans,node* x){
if(x == null) return ;
double c = x->x , d = x->val;
double cost = sqrt((a-c)*(a-c) + (b-d)*(b-d));
if(cost < ans) ans = cost;
if(x->c[0] != null) search(a,b,ans,x->c[0]);
if(x->c[1] != null) search(a,b,ans,x->c[1]);
}
void gao(double x,double y,double &ans) {
node *pre = find_pre(root,y-ans) ;
node *succ = find_succ(root,y+ans);
if(pre == null || succ == null) while(1);
Splay(pre,null);
Splay(succ,root);
search(x,y,ans,KT);
}
void vist(node *x) {
if(x!=null) {
printf("%d x = %lf y = %lf lson=%d rson=%d\n",x->id,x->x,x->val,x->c[0]->id,x->c[1]->id);
if(x->c[0]!=null) vist(x->c[0]);
if(x->c[1]!=null) vist(x->c[1]);
}
}
void debug(){
puts("*******");
vist(root);
puts("*****");
}
}spt;
void prepare() {
null->id = 0;
null->c[0] = null->c[1] = null->fa = NULL;
null->sz = null->val = 0;
top = 0;
}
double x[maxn] , y[maxn];
bool by_x(int a,int b) {
return x[a] > x[b];
}
int id[maxn];
double sqr(double a) { return a * a; }
int main()
{
int n;
while(scanf("%d",&n),n) {
prepare();
spt.init();
for(int i = 0; i < n; i++) {
id[i] = i;
scanf("%lf%lf",&x[i],&y[i]);
}
if(n == 1){
puts("0.00");
continue;
}
std::sort(id,id+n,by_x);
spt.insert(0,-1e100,-1e100);
spt.insert(0,-1e100,1e100);
double ans = 1e50 ;
int pt = 0;
for(int i = 0; i < n; i++) {
if(ans == 0) break;
while(pt < i && x[id[pt]] > x[id[i]] + ans ) {
if(spt.root->sz == 2) break;
spt.Del(ID[id[pt]]);
pt++;
}
if(spt.root->sz == 2) {
spt.insert(id[i],x[id[i]],y[id[i]]);
} else {
spt.gao(x[id[i]],y[id[i]],ans);
spt.insert(id[i],x[id[i]],y[id[i]]);
}
}
printf("%.2f\n",ans*0.5);
}
return 0;
}
hdu 1007 最近点对问题(Splay解法)的更多相关文章
- hdu 1007最近点对问题
先说下题意,很简单,给n个点的坐标,求距离最近的一对点之间距离的一半.第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标,实数. 这个题目其实就是求最近点对的距离.主要思想就是分治.先把 ...
- zoj 2107&&hdu 1007最近点对问题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds ...
- HDU 1007 Quoit Design(二分+浮点数精度控制)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 1007 Quoit Design(经典最近点对问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- HDU 1007 Quoit Design 平面内最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...
- HDU 1007:Quoit Design(分治求最近点对)
http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:平面上有n个点,问最近的两个点之间的距离的一半是多少. 思路:用分治做.把整体分为左右两个部分,那么 ...
- HDU 1007(套圈 最近点对距离)
题意是求出所给各点中最近点对的距离的一半(背景忽略). 用分治的思想,先根据各点的横坐标进行排序,以中间的点为界,分别求出左边点集的最小距离和右边点集的最小距离,然后开始合并,分别求左右点集中各点与中 ...
- hdu 1007 Quoit Design(分治法求最近点对)
大致题意:给N个点,求最近点对的距离 d :输出:r = d/2. // Time 2093 ms; Memory 1812 K #include<iostream> #include&l ...
- HDU 1007 Quoit Design(计算几何の最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
随机推荐
- 【Cron Expressions】Quartz Scheduler 2.1.x 英文节选
Cron Expressions Cron-Expressions are used to configure instances ofCronTrigger. Cron-Expressions ar ...
- MVC客户端验证的小示例
MVC客户端验证的小示例 配置客户端验证的可用性: <configuration> <appSettings> <add key="ClientValidat ...
- 编译kernel:编译
韦东山Linux视频第1期_裸板_UBoot_文件系统_驱动初步第10课第3节 内核启动流程分析之Makefile.WMV 1. 编译内核分三步: make xxx_defconfig [linux ...
- [Android] 更改关联的源码路径
右击选中工程 → Java Build Path → Libraries → Android 4.1.2 → 点开android.jar → 选中Source attachment → Edit,即可 ...
- 使用Java创建RESTful Web Service(转)
REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...
- SQL中on条件与where条件的区别(转载)
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时表时使用的条 ...
- Swift - 使用UISearchController实现带搜索栏的表格
我原来写过一篇文章“Swift - 带结果列表的搜索条(UISearchDisplayController)的用法”,当时是使用UISearchDisplayController来实现带有搜索功能的列 ...
- Spring MVC 多选框 绑定 Entity 中的 list 属性
问题描述: 有两个类:Record.java 和 User.java,Record中有个attenders属性,是List<User>类型. 我想绑定Record中的attenders.网 ...
- Kendo UI开发教程(20): Kendo MVVM 数据绑定(九) Text
Text绑定可以使用ViewModel来设置DOM元素的文本属性,如果需要设置input,textarea,或select的显示,需要使用value属性. 1 <span data-bind=& ...
- 怎样查看apk须要支持的Android版本号
假设有一个apk,须要知道他最低安装支持的Android版本号是什么,应该怎样查看呢? 直接将apk后缀名改为rar或者zip,拉出AndroidManifest.xml?不行,AndroidMani ...