uva 10245 近期点对问题
分治法的典例
当练手了
奇妙的是。使用inplace_merge按说应该是O(n)的算法。可是用sort nlogn的算法反而更快
先上快排版
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int SIZE = 10000+10;
const double INF = 100000; struct Point
{
double x,y;
}p[SIZE],q[SIZE]; int n; inline double dis(const Point a, const Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} bool cmpx(const Point &a, const Point &b)
{
return a.x<b.x;
} bool cmpy(const Point &a, const Point &b)
{
return a.y<b.y;
} double MinDis(int l,int r)
{
if(r-l<1)return INF;
if(r-l==1)return dis(p[r],p[l]); int m=(l+r)/2;
double d=min(MinDis(l,m),MinDis(m+1,r));
int left=l,right=r+1;
int i;
for(i=m;i>=l;i--)
if(p[i].x<p[m].x-d)
{
left=i+1;
break;
}
for(i=m;i<=r;i++)
if(p[i].x>p[m].x+d)
{
right=i;
break;
}
for(int i=left;i<right;i++)q[i]=p[i];
sort(q+left,q+right,cmpy);
int j;
double ret=d;
for(int i=left;i<right;i++)
{
for(j=i+1;j<right && q[j].y-q[i].y<d;j++)
{
ret=min(ret,dis(q[i],q[j]));
}
}
return ret; } int main()
{
double ans; while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpx);
ans=MinDis(0,n-1);
if(ans>10000)printf("INFINITY\n");
else printf("%.4lf\n",ans);
} return 0;
}
再上归并
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int SIZE = 10000+10;
const double INF = 100000;
#define dis(a,b) sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))
#define Fabs(x) x>0?x:-x
const double eps=1e-9; struct Point
{
double x,y;
}p[SIZE],q[SIZE]; int n; /*inline double dis(const Point a, const Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}*/ bool cmpx(const Point &a, const Point &b)
{
return a.x<b.x;
} bool cmpy(const Point &a, const Point &b)
{
return a.y<b.y;
} double MinDis(int l,int r)
{
if(r-l<1)return INF;
//if(r-l==1)return dis(p[r],p[l]); int m=(l+r)/2;
double d=min(MinDis(l,m),MinDis(m+1,r));
inplace_merge(p+l,p+m+1,p+r+1,cmpy);
int j,right=0;
for(int i=l;i<=r;i++)//0 <n
if(p[i].x >= p[m].x-d && p[i].x<=p[m].x+d)
q[right++]=p[i];
double ret=INF;
for(int i=0;i<right;i++)
{
for(j=i+1;j<right && q[j].y-q[i].y<d;j++)
{
ret=min(ret,dis(q[i],q[j]));
}
}
return min(d,ret);
} int main()
{
double ans; while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpx);
ans=MinDis(0,n-1);
if(ans>=10000)printf("INFINITY\n");
else printf("%.4lf\n",ans);
} return 0;
}
uva 10245 近期点对问题的更多相关文章
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- uva 10245 The Closest Pair Problem_枚举
题意:求任意两点之间的距离的最少一个距离 思路:枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath& ...
- UVa 10245 The Closest Pair Problem (分治)
题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...
- UVA 10245 The Closest Pair Problem【分治】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269 题意: 求平面最近点对. 分析: 经典问题. n比 ...
- <算法竞赛入门经典> 第8章 贪心+递归+分治总结
虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...
- UVA - 11354Bond最小生成树,LCA寻找近期公共祖先
看懂题目意思.他的意思是求将全部的城市走一遍,危急度最小.而且给 你两个s,t后让你求在走的时候,从s到t过程中危急度最大的值,并输出它, 然后就是怎样攻克了,这个题目能够说简单,也能够说难 通过思考 ...
- UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...
- UVA 11423 - Cache Simulator (树状数组)
UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...
随机推荐
- Android ARM汇编语言
简介 ARM是Advanced RISC Machine的首字母缩写,它可以称之为一家嵌入式处理器的提供商,也可以理解为一种处理器的架构,还可以将它作为一套完整的处理器指令集. 原生程序与ARM汇编语 ...
- android水平循环滚动控件
CycleScrollView.java package com.example.test; import android.content.Context; import android.graphi ...
- 008实现一个算法从一个单链表中返回倒数第n个元素(keep it up)
我们维护两个指针, 它们之间的距离为n. 然后.我将这两个指针同步地在这个单链表上移动,保持它们的距离 为n不变. 那么, 当第二个指针指到空时.第一个指针即为所求. #include <ios ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- Java程序猿学习当中各个阶段的建议
回答阿里社招面试如何准备,顺便谈谈对于Java程序猿学习当中各个阶段的建议 引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的 ...
- OpenLayers学习笔记4——使用jQuery UI实现測量对话框
OpenLayers学习最好的方式就是跟着其自带的演示样例进行学习,另外对web前端的开发设计要了解,慢慢积累,这样在一般的小项目中应该是足够用了. 本篇參照量測demo实现对话框形式的量測,抛砖引玉 ...
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第二步---编辑器(1)--触摸加入点
/* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦,他说:随便写,第一别全然照搬代码:第二能够说 ...
- .NET开源 FAQ
Microsoft至2014年11月12日本(PST)公布.NET开源.一个"隐居"商业帝国也迎来"改革开放".. . Q1:为什么要开放源码? Ans:由于. ...
- jQuery获取url参数值
$.extend({ getUrlVars: function () { var vars = [], hash; var hashes = window.location.href.slice(wi ...
- 【前段开发】行内元素和块级元素总结(HTML CSS)
块级元素 <address> information on author <blockquote> long quotation <button> push but ...