算法复习——平面分治(hud1007)
题目:
问题描述 :
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
输入:
输出:
样例输入:
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0
样例输出:
0.71
0.00
0.75
题解:


心得:
平面分治经典模板题···核心思想就是按xy坐标排序后分成左右两边分治··复杂度nlogn;
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=1e+;
struct point
{
double x;
double y;
}p[N],px[N];
bool compx(point a,point b)
{
return a.x<b.x;
}
bool compy(point a,point b)
{
return a.y<b.y;
}
inline double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int n;
double work(int l,int r)
{
double ans;
if(l+==r) return dis(p[l],p[r]);
if(l+==r) return min(dis(p[l],p[l+]),min(dis(p[l],p[r]),dis(p[l+],p[r])));
int mid=(l+r)/;
ans=min(work(l,mid),work(mid+,r));
int cnt=;
for(int i=l;i<=r;i++)
{
if(p[i].x>=p[mid].x-ans&&p[i].x<=p[mid].x+ans)
px[++cnt]=p[i];
}
sort(px+,px+cnt+,compy);
for(int i=;i<=cnt;i++)
for(int j=i+;j<=cnt;j++)
{
if(px[j].y-px[i].y>=ans)
break;
ans=min(ans,dis(px[i],px[j]));
}
return ans;
}
int main()
{
freopen("a.in","r",stdin);
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
for(int i=;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p+,p+n+,compx);
printf("%.2lf\n",work(,n)/);
}
return ;
}
算法复习——平面分治(hud1007)的更多相关文章
- 算法复习——cdq分治
题目: Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要 ...
- 算法复习——序列分治(ssoj光荣的梦想)
题目: 题目描述 Prince对他在这片大陆上维护的秩序感到满意,于是决定启程离开艾泽拉斯.在他动身之前,Prince决定赋予King_Bette最强大的能量以守护世界.保卫这里的平衡与和谐.在那个时 ...
- C#冒泡算法复习
C#冒泡算法复习 冒泡算法的意思:每一趟找到一个最小或最大的数放到最后面,比较总数的n-1次(因为比较是2个双双比较的) 第一层循环表示进行比较的次数,总共要比较(数的)-1次 (因为比较是2个双双比 ...
- 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点
平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...
- C语言排序算法复习
排序算法有很多种,这里在复习和分析的基础上,做一个自己的总结: 首先要知道有哪些排序算法,google一下,有云C语言7大经典排序算法(也有8大).主要包括冒泡排序,快速排序,选择排序,插入排序,希尔 ...
- 【从零学习经典算法系列】分治策略实例——高速排序(QuickSort)
在前面的博文(http://blog.csdn.net/jasonding1354/article/details/37736555)中介绍了作为分治策略的经典实例,即归并排序.并给出了递归形式和循环 ...
- Luogu 1429 平面最近点对 | 平面分治
Luogu 1429 平面最近点对 题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 ...
- KMP算法复习【+继续学习】
离NOIP还剩12天,本蒟蒻开始准备复习了. 先来个KMP[似乎我并没有写过KMP的blog] KMP KMP算法是解决字符串匹配问题的一个算法,主要是单对单的字符串匹配加速,时间复杂度O(m + n ...
- 算法复习周------“动态规划之‘最长公共子序列’”&&《计蒜课》---最长公共子串题解
问题描述: 这个问题其实很容易理解.就是给你两个序列X={x1,x2,x3......xm} Y={y1,y2,y3......ym},要求找出X和Y的一个最长的公共子序列. 例:Xi={A, B, ...
随机推荐
- 源文件名长度大于系统支持的长度,无法删除,java主方法执行方式删除
import java.io.File; /** * @author 海盗船长 * 2017年2月14日11:24:26 */ public class DeleteFiles { public st ...
- ORM进阶操作
一.聚合查询:aggregate(*args, **kwargs) aggregate()是QuerySet 的一个终止子句,意思是说,它返回一个包含一些键值对的字典.键的名称是聚合值的标识符,值是计 ...
- Spring IOC的Bean对象
---恢复内容开始--- 在Spring IOC模块中Bean是非常重要的.在这里我想给大家讲讲关于Bean对象实例化的三种注入方式: 首先,我先讲一下关于Bean对象属性值的两种注入方式:set注入 ...
- 使用js将后台返回的数据转换成树形结构
将类似如下数据转换成树形的数据: [ { id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1 ...
- C++类型强制转换<转>
转载:http://www.cnblogs.com/goodhacker/archive/2011/07/20/2111996.html C风格的强制类型转换(Type Cast)很简单,不管什么类型 ...
- Java生成固定长度的随机字符串(以大小写字母和数字)
package org.jimmy.autosearch2019.test; import java.util.ArrayList; import java.util.Random; /** * @a ...
- strchr函数
函数原型:extern char *strchr(char *str,char character) 参数说明:str为一个字符串的指针,character为一个待查找字符. 所在库名 ...
- Apache Commons Configuration的应用
Apache Commons Configuration的应用 Commons Configuration是一个java应用程序的配置管理工具.可以从properties或者xml文件中加载软件的配置 ...
- SimpleWeather APP
参考 iOS 7 Best Practices; A Weather App Case Study: Part 1/2 iOS 7 Best Practices; A Weather App Case ...
- ios之UIActionSheet
UIActionSheet是在IOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件. 为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view applicatio ...