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 ...
随机推荐
- SQL Server 修改AlwaysOn共享网络位置
标签:MSSQL/故障转移 概述 很多人一开始搭建Alwayson的时候对于共享网络位置的选择不是很重视, 导致后面需要去修改这个路径.但是怎样修改这个路径呢?貌似没有给出具体的修改选项,但是还是有地 ...
- 【Zookeeper】源码分析之服务器(五)之ObserverZooKeeperServer
一.前言 前面分析了FollowerZooKeeperServer,接着分析ObserverZooKeeperServer. 二.ObserverZooKeeperServer源码分析 2.1 类的继 ...
- 瞎j8封装第二版之用xml文件来代理dao接口
也是重新整理了之前的那篇 模仿Mybatis用map per.xml实现Dao层接口的功能 话不多说直接上代码 首先是结构 依赖pom.xml <?xml version="1.0&q ...
- android v4兼容包
一句话解释android兼容包就是:支持更多的组件,样式更好看了.好粗糙的解释啊! 我们都知道Android一些SDK比较分裂,为此google官方提供了Android Support Library ...
- Swift学习第一天--面向过程
//: Playground - noun: a place where people can play import UIKit //---------------------- Hello wor ...
- 安装MongoDB步骤
1.第一步是从官网下载匹配自己操作系统的安装文件或压缩文件: 2.随便找个文件夹先解压安装文件,然后在C盘根目录建立一个新文件夹命名为mongodb: 3.将打开刚刚安装的文件,将bin文件夹拷贝到C ...
- IT服务(运维)管理实施的几个要点--序言
IT服务(运维)管理(不是IT运维技术)是IT行业当中相对比较"窄"的一个分支,通常只被金融.电信等大型数据中心的中高层管理人员所关注.但是根据笔者多年从事IT服务和服务管理的经验 ...
- JavaScript的简单入门
一.导读 简介:JavaScript简称js,是基于对象和事件驱动的脚本语言,主要运用于客户端.原名LiveScript,本身和Java没有任何关系,但语法上很类似. 特点:交互性(它可以做的就是信息 ...
- ActiveMQ (一) 初识ActiveMQ
了解JMS JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进 ...
- JS输出26个英文大小写字母
JS中可以利用ASCII值 for(var i=0;i<26;i++){ console.log(String.fromCharCode(65+i));//输出A-Z 26个大写字母 } for ...