Quoit Design(最近点对+分治)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007
Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42865 Accepted Submission(s): 11128
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.
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0
0.00
0.75
最近点对问题:
分治的思想,如果是大于3个点的时候,将所有的点按照x坐标(或者y坐标)排序,然后从中间线一份两半,分别求出左边的点的最近点距ldis,和右边点的最近点距rdis,那么问题就是要将两个部分的点合起来,令dis = min(rdis,ldis);那么中间如果想出现最近点对的时候必须要出现在中间线两侧距离为d的区域内,那么考虑区域内的左侧的点,与其产生最近点的点一定在坐标的右侧,那么为了不用枚举区域内右边所有的点,就要考虑对于每个点它如果要产生距离小于dis的对应点,肯定是在以它为圆心的dis为半径的园内,那么这个圆覆盖这个带状区域面积最大的情况就是圆心在分界线上的时候。如图


注意:在处理中间带的时候将带状区域内的点按照y坐标排序(如果之前是按照y排序的,则现在按照x坐标排序)那么因为右侧的任意两点之间的距离要大于等于dis所以要想极限情况,就是在这个圆区域内找等边三角形,因为从上到下处理的点,所以当前点的上方的点不在考虑,由于排序的时候是左右两边的点一起排序的所以要在当前点的编号向后处理7个点,如上图中划出的三个点,如果发现他们来自同一侧则不再处理
下面是模板代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std; #define N 100005
const int L = -;
const int R = ;
struct Node{
int id;
double x;
double y;
};
Node num[N],cp[N]; //double Dis(Node a, Node b){
// return sqrt(pow((a.x - b.x),2.0)+pow((a.y-b.y),2.0));
//}
double Dis(Node a, Node b){
double x = a.x-b.x, y = a.y-b.y;
return sqrt(x*x+y*y);
} bool cmpx(Node a, Node b)
{
if(a.x==b.x) return a.y<b.y;
else return a.x < b.x;
}
bool cmpy(Node a, Node b)
{
if(a.y==b.y) return a.x<b.x;
else return a.y<b.y;
}
double solve(int low, int high)
{
double dis;
int sum = high - low;
if(sum == ){ return ; }//只有一个数
else if(sum == ){//两个数
dis = Dis(num[low],num[high]);
}
else if(sum == ){//三个数
double tm1,tm2,tm3;
tm1 = Dis(num[low],num[low+]);
tm2 = Dis(num[low+],num[high]);
tm3 = Dis(num[low],num[high]);
dis = min(tm1,min(tm2,tm3));
}
else //大于三个数
{
double lmin,rmin,mmin;
int mid = (low+high)/;
int p = ;
int i, j;
lmin = solve(low,mid);
rmin = solve(mid+,high);
dis = min(lmin,rmin);
/**-----------------提出来会变快-----------------*/
double ldis = num[mid].x-dis;
double rdis = num[mid].x+dis;
for( i = low; i <= mid; i++) /**-----小于等于,不能是小于,因为下面标记 L R 了*/
{
if(num[i].x >= ldis)
{
cp[p].id = L;//标记为属于左边的部分
cp[p].x = num[i].x;
cp[p].y = num[i].y;
p++;
}
}
for( ; i <= high; i++)
{
if(num[i].x <= rdis)
{
cp[p].id = R;//标记为右边的点
cp[p].x = num[i].x;
cp[p].y = num[i].y;
p++;
}
}
sort(cp,cp+p,cmpy);
for( i = ; i < p; i++)
{
for( j = ; (j <= )&&(i+j)<p; j++)
{
if(cp[i].id != cp[i+j].id)//最小值可能出现在分界线不同的两边
{
mmin = Dis(cp[i],cp[i+j]);
if(mmin<dis)
dis = mmin;
}
}
}
}
return dis;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n!=)
{
double result = ;
for(int i = ; i < n; i++)
{
num[i].id = ;
scanf("%lf%lf",&num[i].x,&num[i].y);
}
sort(num,num+n,cmpx);
result = solve(,n-);
printf("%.2f\n",result/);
}
return ;
}
下面是一开始wa的代码:
对应的错误在上面代码中用用/** -------*标识
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 100005
const int L = -;
const int R = ;
struct Node{
int id;
double x;
double y;
};
Node num[N],cp[N]; double Dis(Node a, Node b){
return sqrt(pow((a.x - b.x),2.0)+pow((a.y-b.y),2.0));
} bool cmpx(Node a, Node b)
{
if(a.x==b.x) return a.y<b.y;
else return a.x < b.x;
}
bool cmpy(Node a, Node b)
{
if(a.y==b.y) return a.x<b.x;
else return a.y<b.y;
} double solve(int low, int high)
{
double dis;
int sum = high - low;
if(sum == ){ return ; }//只有一个数
else if(sum == ){//两个数
dis = Dis(num[low],num[high]);
}
else if(sum == ){//三个数
double tm1,tm2,tm3;
tm1 = Dis(num[low],num[low+]);
tm2 = Dis(num[low+],num[high]);
tm3 = Dis(num[low],num[high]);
dis = min(tm1,min(tm2,tm3));
}
else //大于三个数
{
double lmin,rmin,mmin;
int mid = (low+high)/;
int p = ;
int i, j;
lmin = solve(low,mid);
rmin = solve(mid+,high);
dis = min(lmin,rmin);
for( i = low; i < mid; i++)
{
double ldis = num[mid].x - dis;
if(num[i].x >= ldis)
{
cp[p].id = L;//标记为属于左边的部分
cp[p].x = num[i].x;
cp[p].y = num[i].y;
p++;
}
}
for( ; i < high; i++)
{
double rdis = num[mid].x+dis;
if(num[i].x <= rdis)
{
cp[p].id = R;//标记为右边的点
cp[p].x = num[i].x;
cp[p].y = num[i].y;
p++;
}
}
sort(cp,cp+p,cmpy);
for( i = ; i < p; i++)
{
for( j = ; (j <= )&&(i+j)<p; j++)
{
if(cp[i].id != cp[i+j].id)//最小值可能出现在分界线不同的两边
{
mmin = Dis(cp[i],cp[i+j]);
if(mmin<dis)
dis = mmin;
}
}
}
}
return dis;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n!=)
{
double result = ;
for(int i = ; i < n; i++)
{
num[i].id = ;
scanf("%lf%lf",&num[i].x,&num[i].y);
}
sort(num,num+n,cmpx);
result = solve(,n-);
printf("%.2f\n",result/);
}
return ;
}
下面给出大神的模板代码:(虽然内容差不多,感觉一下代码风格)
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100007 using namespace std; struct Point
{
double x,y;
}pt[N];
int a[N]; int n; bool cmp(Point a, Point b)
{
if (a.x != b.x) return a.x < b.x;
else return a.y < b.y;
}
bool cmp_y(int id1, int id2){
return pt[id1].y < pt[id2].y;
}
double getDis(const Point &a, const Point &b)
{
double x = a.x - b.x;
double y = a.y - b.y;
return sqrt(x*x + y*y);
}
double solve(int l, int r)
{
double ans = ;
if (r - l + <= )
{
if (r - l + == ) return ans;
ans = getDis(pt[l], pt[l + ]);
if (r - l + == ) return ans;
for (int i = l; i < r; ++i)
{
for (int j = i + ; j <= r; ++j)
{
ans = min(ans, getDis(pt[i],pt[j]));
}
}
return ans;
}
int m = (l + r) >> ;
double s1 = solve(l, m);
double s2 = solve(m + , r);
ans = min(s1,s2);
int k = ;
for (int i = m - ; i >= l && pt[m].x - pt[i].x <= ans; --i) a[k++] = i;
for (int i = m + ; i <= r && pt[i].x - pt[m].x <= ans; ++i) a[k++] = i;
sort(a, a + k, cmp_y);
for (int i = ; i < k; ++i)
{
for (int j = i + ; j < k && j <= i + ; ++j)
{
ans = min(ans, getDis(pt[a[i]], pt[a[j]]));
}
}
return ans;
}
int main()
{
while (~scanf("%d",&n))
{
if (!n) break;
for (int i = ; i < n; ++i) scanf("%lf%lf",&pt[i].x, &pt[i].y);
sort(pt, pt + n, cmp);
printf("%.2lf\n",solve(, n - )/2.0);
}
return ;
}
Quoit Design(最近点对+分治)的更多相关文章
- hdu 1007 Quoit Design (最近点对问题)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 杭电OJ——1007 Quoit Design(最近点对问题)
Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...
- ZOJ2107 Quoit Design 最近点对
ZOJ2107 给定10^5个点,求距离最近的点对的距离. O(n^2)的算法是显而易见的. 可以通过分治优化到O(nlogn) 代码很简单 #include<iostream> #inc ...
- HDU 1007 Quoit Design最近点对( 分治法)
题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...
- HDU 1007 Quoit Design【计算几何/分治/最近点对】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- ZOJ 2017 Quoit Design 经典分治!!! 最近点对问题
Quoit Design Time Limit: 5 Seconds Memory Limit: 32768 KB Have you ever played quoit in a playg ...
- hdu 1007 Quoit Design 分治求最近点对
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1007 Quoit Design(经典最近点对问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- HDU1007 Quoit Design 【分治】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- 网口划VLAN
do sho run int g0/28 int g0/18 sw mo acc sw acc vlan 220 span portfa exit do wr exit
- bzoj 3894: 文理分科
Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行 描述,每个格子代表一个同学的座位. ...
- su 和 sudo 命令的区别-转载
link 一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要 ...
- 从底层角度看ASP.NET-A low-level Look at the ASP.NET...
从更低的角度 这篇文章在一个底层的角度来关注一个web请求怎样到达asp.net框架,从web服务器,通过ISAPI.看看这些后面发生了什么,让我们停止对asp.net的黑箱猜想.ASP.NET是一个 ...
- css3整理-方便查询使用
最近详细地研究了CSS3的相关内容,并整理了这个文档,方便以后查询使用,分享给大家. 案例代码大家可以下载参考下:https://gitee.com/LIULIULIU8/CSS3 1.边框属性bor ...
- ThreadLocal 线程本地变量 及 源码分析
■ ThreadLocal 定义 ThreadLocal通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量 ...
- Python个人项目--豆瓣图书个性化推荐
项目名称: 豆瓣图书个性化推荐 需求简述:从给定的豆瓣用户名中,获取该用户所有豆瓣好友列表,从豆瓣好友中找出他们读过的且评分5星的图书,如果同一本书被不同的好友评5星,评分人数越多推荐度越高. 输入: ...
- Linux如何让进程在后台运行的三种方法详解
问题分析: 我们知道,当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程.因此,我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运 ...
- java 集合类基础问题汇总
1.Java集合类框架的基本接口有哪些? 参考答案 集合类接口指定了一组叫做元素的对象.集合类接口的每一种具体的实现类都可以选择以它自己的方式对元素进行保存和排序.有的集合类允许重复的键,有些不允许 ...
- Netty入门之HelloWorld
Netty系列入门之HelloWorld(一) 一. 简介 Netty is a NIO client server framework which enables quick and easy de ...