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 ...
随机推荐
- Mybatis入门(一)之操作数据库
Whats Mybatis 持久层框架, 替代MVC层中DAO,因为DAO 层的需求就是 :能与数据库交互的对象. 能执行SQL语句. 不同于JDBC的connection,MyBatis 中有个Sq ...
- Java多线程之赛跑游戏
在JavaSE中,多线程是一个重要的内容. 我们要了解多线程的概念,就要先了解进程的概念:要了解进程的概念,就离不开操作系统的概念. 在一台正常运行的电脑中,计算机硬件(如CPU.内存.硬盘.网卡.显 ...
- CSS 水平居中/布局 垂直居中 (月经问题)
水平居中 如果它是一个行内元素 对其父元素使用 text-align:center 即可实现. <p style = " text-align:center; width:300px; ...
- Quartz.Net 使用
Quartz.NET 是一套很好的任务调度框架. 下面介绍如何使用: 在项目Nuget包管理器中搜索:quartz 安装后会添加如下dll: <packages> <package ...
- 线程安全Dictionary
public abstract class ReadFreeCache<TKey, TValue> { protected ReadFreeCache() : this(null) { } ...
- CentOS7下安装MySQL并配置远程连接
一.CentOS7下安装MySQL数据库 CentOS7默认的安装包里面已经没有 MySQL-Server安装包了,远程镜像中也没有了. 默认的是MariaDB (MySQL的一个分支,开发这个分支的 ...
- SSM中(Spring-SpringMVC-Mybatis)(二:整合)
使用Maven整合一个SSM,当然要配有Maven环境,Maven的环境配置自行百度,挺好配的 创建一个Maven项目以后,目录结构如图所示: 然后就是各部分配置: 首先pom导入依赖jar包: &l ...
- MyBatis单个多个参数传递
一.传入单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList ...
- MicroPython开发板:TPYBoard v102 播放音乐实例
0x00前言 前段时间看到TPYBoard的技术交流群(群号:157816561,)里有人问关于TPYBoard播放音乐的问题.最近抽空看了一下文档介绍,着手做了个实验.更多MicroPython的教 ...
- Python Tornado篇
Tornado既是一个web server,也是web framework.而它作为web server 采用的是asynchronous IO的网络模型,这是一种很高效的模型. Tornado 和现 ...