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 ...
随机推荐
- Android数组和开发List之间的转换
1.List转换到一个数组.(这里List它是实体是ArrayList) 转让ArrayList的toArray方法. toArray public <T> T[] toArray(T[] ...
- Android Intent传递对象摘要
效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG9uZ3NoZW5ncGVuZw==/font/5a6L5L2T/fontsize/400/fil ...
- OA 框架
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport&q ...
- LeetCode - 4 - Longest Substring Without Repeating Characters
题目 URL:https://leetcode.com/problems/median-of-two-sorted-arrays/ 解法 二分法. 总的思想是将 2 个数组用 2 个指针“整体”二分. ...
- 运行control userpasswords2实现winXP自动登录
原文:运行control userpasswords2实现winXP自动登录 如果你的计算机只是自己一人在用,且每次都用同一个用户名(或者你根本没在意过什么是用户名),而每次都要输入密码,是否太麻烦了 ...
- Qt中连接到同一signal的多个slots的执行顺序问题(现在是看连接顺序,以前是无序的)
in the order they have been connected 起源 前些天忘记在哪儿讨论过这个问题,今天在csdn又看到有网友问这个问题,而其他网友却无一例外的给出了“无序”这个答案. ...
- Qt 访问网络的 HttpClient(封装QNetworkAccessManager,且有服务端)
Qt 使用 QNetworkAccessManager 访问网络,这里对其进行了简单的封装,访问网络的代码可以简化为: 1 2 3 HttpClient("http://localhost: ...
- 【原创】ABAP根据文件路径获取文件所在目录
*&---------------------------------------------------------------------* *& Form frm_get_pat ...
- Spring Boot入门篇(基于Spring Boot 2.0系列)
1:概述: Spring Boot是用来简化Spring应用的初始化开发过程. 2:特性: 创建独立的应用(jar|war形式); 需要用到spring-boot-maven-plugin插件 直接嵌 ...
- 快速开发第一个SpringBoot应用
通过笔者这段实践SpringBoot的学习,发现自从使用了SpringBoot后,就再也回不去SpringMVC了,因为相比于SpringMVC,SpringBoot真是太高效率了.下面我们看看它效率 ...