HDU 1007:Quoit Design(分治求最近点对)
http://acm.hdu.edu.cn/showproblem.php?pid=1007
题意:平面上有n个点,问最近的两个点之间的距离的一半是多少。
思路:用分治做。把整体分为左右两个部分,那么有三种情况:最近的两个点都在左边,最近的两个点都在右边和最近的两个点一个在左边一个在右边。对于第一第二种情况,直接递归处理,分解成子问题就好了,主要是要处理第三种情况。最暴力的做法是O(n^2)的扫,这样肯定会TLE。那么要作一些优化。首先我们先递归处理得到第一种和第二种情况的答案的较小值,然后用这个答案去优化,即如果x上,某个点距离中点的距离在ans内的话,那么这个点是可能可以得到更优答案的,如果距离大于ans,那么肯定不能得到更优的答案。将这些点存起来,然后对y进行排序,暴力O(n^2)扫这些存起来的点,和第一个优化类似,如果当前两点之间y的距离大于等于ans,那么后面的答案肯定是大于ans的,直接break跳出去。
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f
struct node {
double x, y;
} p[N], tmp[N]; double min(double a, double b) { return a < b ? a : b; } bool cmpx(const node &a, const node &b) { return a.x < b.x; } bool cmpy(const node &a, const node &b) { return a.y < b.y; } double cal(node a, node b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double solve(int l, int r) {
if(r - l == ) return cal(p[r], p[l]);
if(r - l == ) return min(cal(p[l], p[r]), min(cal(p[r], p[r-]), cal(p[r-], p[l])));
int mid = (l + r) >> , cnt = ;
double ans = min(solve(l, mid), solve(mid + , r));
for(int i = l; i <= r; i++)
if(p[i].x - ans <= p[mid].x && p[i].x + ans >= p[mid].x)
tmp[++cnt] = p[i];
sort(tmp + , tmp + + cnt, cmpy);
for(int i = ; i <= cnt; i++)
for(int j = i + ; j <= cnt; j++)
if(tmp[j].y - tmp[i].y >= ans) break;
else ans = min(ans, cal(tmp[i], tmp[j]));
return ans;
} int main() {
int n;
while(scanf("%d", &n), n) {
for(int i = ; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
sort(p + , p + + n, cmpx);
printf("%.2f\n", solve(, n) / 2.0);
}
return ;
}
HDU 1007:Quoit Design(分治求最近点对)的更多相关文章
- 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 ...
- HDU 1007 Quoit Design 平面内最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...
- HDU 1007 Quoit Design(计算几何の最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- hdu 1007 Quoit Design(平面最近点对)
题意:求平面最近点对之间的距离 解:首先可以想到枚举的方法,枚举i,枚举j算点i和点j之间的距离,时间复杂度O(n2). 如果采用分治的思想,如果我们知道左半边点对答案d1,和右半边点的答案d2,如何 ...
- 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(分治法求最近点对)
大致题意:给N个点,求最近点对的距离 d :输出:r = d/2. // Time 2093 ms; Memory 1812 K #include<iostream> #include&l ...
- 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(二分+浮点数精度控制)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- (hdu1007)Quoit Design,求最近点对
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
随机推荐
- SQL Server查询当前连接数
行数就是连接数,每一行是连接详情 SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT [DBID] FROM [M ...
- Asp UserInfoList 方法二
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.a ...
- linux下计划任务学习记录
0x01 计划任务简介 linux 中计划任务主要分为”循环执行”和”只执行一次”两种,分别对应的时 crond 服务 和 atd 服务: 0x02 只执行一次的计划任务 0x02.1 atd 服务说 ...
- Reverse Engineering Custom DataTypes -> GUID() in SQL Server to PostgreSQL
原文 https://archive.sap.com/discussions/thread/3641585 First you reverse engineer from a script where ...
- 零元学Expression Blend 4 - Chapter 29 ListBox与Button结合运用的简单功能
原文:零元学Expression Blend 4 - Chapter 29 ListBox与Button结合运用的简单功能 本章所讲的是运用ListBox.TextBox与Button,做出简单的列表 ...
- CentOS 7使用yum快速搭建LAMP环境
1.安装Apache [root@localhost ~]# yum -y install httpd # 开机自启动 [root@localhost ~]# chkconfig httpd on # ...
- Android零基础入门第30节:两分钟掌握FrameLayout帧布局
原文:Android零基础入门第30节:两分钟掌握FrameLayout帧布局 前面学习了线性布局.相对布局.表格布局,那么本期来学习第四种布局--FrameLayout帧布局. 一.认识FrameL ...
- How to setup Assigned Access in Windows 10 (Kiosk Mode) 设置分配的访问权限(Kiosk模式)
Let’s say you’re building some sort of ingenious mechanical contraption to be displayed in public th ...
- .NET DataTable转换为JSON格式的字符串
在进行数据传递的时候,有时我们需要通过Ajax的方式或者其他的方式传递一个数据列表,可以将DataTable或者其他形式的数据列表转换为JSON的格式,通过Ajax实体的形式进行传递. 比如说: // ...
- 论文阅读计划2(Deep Joint Rain Detection and Removal from a Single Image)
Deep Joint Rain Detection and Removal from a Single Image[1] 简介:多任务全卷积从单张图片中去除雨迹.本文在现有的模型上,开发了一种多任务深 ...