为什么要写这个题、、经典啊,当然,别以为我用分治做的,不过主要思想还是那神奇的六个点共存(一个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解法)的更多相关文章

  1. hdu 1007最近点对问题

    先说下题意,很简单,给n个点的坐标,求距离最近的一对点之间距离的一半.第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标,实数. 这个题目其实就是求最近点对的距离.主要思想就是分治.先把 ...

  2. zoj 2107&&hdu 1007最近点对问题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds   ...

  3. HDU 1007 Quoit Design(二分+浮点数精度控制)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. HDU 1007 Quoit Design(经典最近点对问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...

  5. HDU 1007 Quoit Design 平面内最近点对

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...

  6. HDU 1007:Quoit Design(分治求最近点对)

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:平面上有n个点,问最近的两个点之间的距离的一半是多少. 思路:用分治做.把整体分为左右两个部分,那么 ...

  7. HDU 1007(套圈 最近点对距离)

    题意是求出所给各点中最近点对的距离的一半(背景忽略). 用分治的思想,先根据各点的横坐标进行排序,以中间的点为界,分别求出左边点集的最小距离和右边点集的最小距离,然后开始合并,分别求左右点集中各点与中 ...

  8. hdu 1007 Quoit Design(分治法求最近点对)

    大致题意:给N个点,求最近点对的距离 d :输出:r = d/2. // Time 2093 ms; Memory 1812 K #include<iostream> #include&l ...

  9. HDU 1007 Quoit Design(计算几何の最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

随机推荐

  1. 11gOCP 1z0-052 :2013-09-11 MGR_ROLE role........................................................A66

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11584537 正确答案A 实验测试 1.创建用户:SKD gyj@OCM> crea ...

  2. UITableViewHeaderFooterView的使用+自己主动布局

    UITableViewHeaderFooterView的使用+自己主动布局 使用UITableView的header或footer复用时,假设採用自己主动布局,你会发现有约束冲突,以下这样写能够消除约 ...

  3. HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树

    lca的做法还是非常明显的.简单粗暴, 只是不是正解.假设树是长链就会跪,直接变成O(n).. 最后跑的也挺快,出题人还是挺阳光的.. 动态树的解法也是听别人说能ac的.预计就是放在splay上剖分一 ...

  4. SQL Server 存储过程、触发器、游标

    存储过程 1.存储过程是事先编好的.存储在数据库中的程序,这些程序用来完成对数据库的指定操作. 2.系统存储过程: SQL Server本身提供了一些存储过程,用于管理有关数据库和用户的信息. 用户存 ...

  5. C++内存管理(超长)

    [导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...

  6. “/” 应用程序中的服务器错误 - IIS 发布错误

    解决方法, 将bin目录下的全部文件复制到iis下

  7. 写一个函数int get(),这个函数运行一次可以从V[N]里随机取出一个数,而这个数必须是符合1/N平均分布的

    题目:有一个函数int getNum(),每运行一次可以从一个数组V[N]里面取出一个数,N未知,当数取完的时候,函数返回NULL.现在要求写一个函数int get(),这个函数运行一次可以从V[N] ...

  8. Swift - 使用UISearchController实现带搜索栏的表格

    我原来写过一篇文章“Swift - 带结果列表的搜索条(UISearchDisplayController)的用法”,当时是使用UISearchDisplayController来实现带有搜索功能的列 ...

  9. 解决php下多人同时操作数据表

    当同一时刻,多人对同一个表进行insert或者update的时候,往往会出现同一条数据出现好多次或者一些奇怪的问题,可以通过mysql的锁表机制来进行排队解决这个问题 php中插入数据之前锁表 // ...

  10. 你以为在用SharePoint但事实上不是

    博客地址 http://blog.csdn.net/foxdave 原文链接:http://www.techrepublic.com/blog/tech-decision-maker/you-thin ...