HDU 1007Quoit Design(最近点问题)

最近点问题:二维平面中有n(n很大)个点,求出距离最近的两个点

	思路:因为n的值很大,所以暴力和dp都行不通了吧!分治法就挺好的。
	将区间一半一半的分开,直到分成只有一个点或两个点的时候!
	对于只有两个点的区间,最小值就是这两个点的距离,只有一个点的区间,
	最小值就是无穷大。注意还要考虑合并的时候,可能距离最近的两个点,
	分别在左右两个不同的区间。对于这种情况的处理如下:
		 mid=(ld+rd)/2;
		 ans = min(solve(ld, mid), solve(mid+1, rd));得到两段区间最小值的最小值
		从中间向两边寻找,因为我们是按照x坐标排序的,在左区间向左边寻找的时候
		如果某一个点的x到中间点x的距离大于ans(否则将这样的点保存),那么这个
		点左边的点就不可能在右区间寻找到相应的点满足两个点的距离小于ans的,那么
		就结束继续查找(这样算是一种优化) 

		同理在右区间向右寻找。。。

		然后对存储的节点按照y坐标进行从小到大的排序。
		枚举每两个点寻找最小的距离
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define MAX 99999999999999.0
 7 using namespace std;
 8
 9 struct node{
10     double x, y;
11 }nd[100005], ndx[100005];
12
13 bool cmp(node a, node b){
14     if(a.x == b.x) return a.y < b.y;
15     return a.x < b.x;
16 }
17
18 bool cmpy(node a, node b){
19     return a.y < b.y;
20 }
21
22 double dist(node a, node b){
23     return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
24 }
25
26 double solve(int ld, int rd){
27     if(ld == rd) return MAX;
28     if(ld + 1 == rd) return dist(nd[ld], nd[rd]);
29     int mid = (ld+rd)/2;
30     double ans = min(solve(ld, mid), solve(mid+1, rd));
31     int len = 0;
32     for(int i = mid; i>=ld; --i)
33         if(nd[mid].x - nd[i].x <= ans)
34             ndx[len++] = nd[i];
35         else break;
36     for(int i=mid+1; i<=rd; ++i)
37         if(nd[i].x - nd[mid].x <= ans)
38             ndx[len++] = nd[i];
39         else break;
40
41     sort(ndx, ndx+len, cmpy) ;
42     for(int i=0; i<len-1; ++i)
43         for(int j=i+1; j<len; ++j)
44             if(ndx[j].y - ndx[i].y >= ans) break;//这里做一处优化
45             else ans = min(ans, dist(ndx[i], ndx[j]));
46     return ans;
47 }
48
49 int main(){
50     int n;
51     while(scanf("%d", &n) && n){
52         for(int i=0; i<n; ++i)
53             scanf("%lf%lf", &nd[i].x, &nd[i].y);
54         sort(nd, nd+n, cmp);
55         printf("%.2lf\n", solve(0, n-1)/2.0);
56     }
57     return 0;
58 }
时间: 2024-12-24 20:05:30

HDU 1007Quoit Design(最近点问题)的相关文章

HDU 1031 Design T-Shirt 选前k大

相当于给出一组数列,然后选择前K大的数的算法. 本题没有给出详细的数据,故此就使用动态分配空间的方法了. 而这种题最好的算法就是使用快排思想,期望时间效率就是O(n)了. 最基本入门解决这种题的算法是直接排序了.那就成了水代码了.用上快排的思想才能体现出水平. 不过这种快排实在考的太多了,建议一定要掌握. 每次做这个算法的题目总会要调试一定时间的,每次都出现奇葩的错误.看来还是不够细心. 做题的时候一定要排除杂念,有干扰,后果很严重,会花长很多时间. 靖空间做题,一定要静,达到一种禅的境界.说禅

ZOJ 2107 HDU 1007 Quoit Design(最近点对)

最近点对的裸题 利用分治去搞搞即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int N = 100005; struct Point { double x, y; void read() { scanf("%lf%lf", &x, &y); } }; b

最近点对问题 HDU Quoit Design 1007 分治法

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<stack> #include<string> #include<queue> using namespace std; const int maxn = 100005; const long long INF =

HDU 1031.Design T-Shirt【结构体二次排序】【8月21】

Design T-Shirt Problem Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA found that he was trapped by all kinds of suggestions from everyone on the board. It is indeed a mission-impossible to have eve

HDOJ 1007 Quoit Design 最近点对

3年前刚接触ACM的时候做的这一题,交了几十次怎么写都过不了......... 今天无意中又看到了这一题,随手一写终于过了..... Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33151    Accepted Submission(s): 8709 Problem Description Have y

杭电 HDU 1031 Design T-Shirt

Design T-Shirt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6527    Accepted Submission(s): 3061 Problem Description Soon after he decided to design a T-shirt for our Algorithm Board on Free

HDU 1031 Design T-Shirt

题意:讲的是XKA要设计T-Shirt,征集大众对各元素的满意度.分别输入3个整数,分别给N.M.K,其中N代表参与打分的人数,M代表元素总数量,K代表XKA所要选用的元素数量.XKA将选用分值较高的前K个元素,若分值相同则选择索引小的的元素.思路:记录每个M的总分数和索引,用结构体存起来,两次排序就够了 另外要注意的输出格式,不能有多余的空格. 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4

ZOJ2107 Quoit Design 最近点对

ZOJ2107 给定10^5个点,求距离最近的点对的距离. O(n^2)的算法是显而易见的. 可以通过分治优化到O(nlogn) 代码很简单 #include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> #include<ve

HDU - 1007 平面最近点对

Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded. In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it ca