解题思路:

分治法求平面近期点对。点分成两部分,加个标记就好了。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <vector>
  7. #include <queue>
  8. #include <algorithm>
  9. #include <iomanip>
  10. #include <string.h>
  11. #define LL long long
  12. using namespace std;
  13. const int MAXN = 200000 + 10;
  14. const double INF = 1e100;
  15. struct Point
  16. {
  17. double x, y;
  18. int flag;
  19. }P[MAXN];
  20. int N;
  21. Point vec[MAXN];
  22. bool cmp_x(Point a, Point b)
  23. {
  24. return a.x < b.x;
  25. }
  26. bool cmp_y(Point a, Point b)
  27. {
  28. return a.y < b.y;
  29. }
  30. double dis(Point a, Point b)
  31. {
  32. double dx = a.x - b.x;
  33. double dy = a.y - b.y;
  34. return sqrt(dx * dx + dy * dy);
  35. }
  36. double solve(Point *a, int l, int r)
  37. {
  38. if(l == r) return INF;
  39. if(l + 1 == r)
  40. {
  41. if(P[l].flag == P[r].flag)
  42. return INF;
  43. return dis(P[l], P[r]);
  44. }
  45. int m = (l + r) >> 1;
  46. double d = solve(a, l, m);
  47. d = min(d, solve(a, m + 1, r));
  48. int sz = 0;
  49. for(int i=l;i<=r;i++)
  50. {
  51. if(fabs(P[i].x - P[m].x) <= d)
  52. vec[sz++] = P[i];
  53. }
  54. sort(vec, vec + sz, cmp_y);
  55. for(int i=0;i<sz;i++)
  56. {
  57. for(int j=i+1;j<sz;j++)
  58. {
  59. if(fabs(vec[i].y - vec[j].y) >= d)
  60. break;
  61. if(vec[i].flag != vec[j].flag)
  62. {
  63. double rs = dis(vec[i], vec[j]);
  64. if(rs < d) d = rs;
  65. }
  66. }
  67. }
  68. return d;
  69. }
  70. int main()
  71. {
  72. int T;
  73. scanf("%d", &T);
  74. while(T--)
  75. {
  76. scanf("%d", &N);
  77. for(int i=0;i<N;i++)
  78. {
  79. scanf("%lf%lf", &P[i].x, &P[i].y);
  80. P[i].flag = 0;
  81. }
  82. for(int i=0;i<N;i++)
  83. {
  84. scanf("%lf%lf", &P[i + N].x, &P[i + N].y);
  85. P[i + N]. flag = 1;
  86. }
  87. N <<= 1;
  88. sort(P, P + N, cmp_x);
  89. double ans = solve(P, 0, N - 1);
  90. printf("%.3f\n", ans);
  91. }
  92. return 0;
  93. }

POJ 3714 Raid(平面近期点对)的更多相关文章

  1. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

  2. poj 3714 Raid(平面最近点对)

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7473   Accepted: 2221 Description ...

  3. POJ 3714 Raid 近期对点题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  4. POJ 3714 Raid

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  5. poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】

    题目:  http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...

  6. POJ 3714 Raid(计算几何の最近点对)

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  7. (洛谷 P1429 平面最近点对(加强版) || 洛谷 P1257 || Quoit Design HDU - 1007 ) && Raid POJ - 3714

    这个讲的好: https://phoenixzhao.github.io/%E6%B1%82%E6%9C%80%E8%BF%91%E5%AF%B9%E7%9A%84%E4%B8%89%E7%A7%8D ...

  8. 【POJ 3714】 Raid

    [题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...

  9. 【POJ 3714】Raid

    [题目链接]:http://poj.org/problem?id=3714 [题意] 给你两类的点; 各n个; 然后让你求出2*n个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...

随机推荐

  1. select into from 与 insert into select 区别

    1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tab ...

  2. 基于cropper.js的图片上传和裁剪

    项目中要求图片上传并裁剪的功能,之前也有接触过很多图片裁剪插件,效果体验不是很好,今天推荐一款好用的插件-cropper,超级好用,裁剪功能丰富,满足了各种需求. 功能: 1:点击选择图片,弹出文件夹 ...

  3. 洛谷 P2368 EXCEEDED WARNING B

    P2368 EXCEEDED WARNING B 题目背景 SGU 107 题目描述 求有多少个平方后末尾为987654321的n位数 输入输出格式 输入格式: 整数n 输出格式: 答案,即[b]“平 ...

  4. ASP.NET MVC 入门4、Controller与Action

    原帖地址:http://www.cnblogs.com/QLeelulu/archive/2008/10/04/1303672.html Controller是MVC中比較重要的一部分.差点儿全部的业 ...

  5. 基于深度学习的人脸识别系统(Caffe+OpenCV+Dlib)【一】如何配置caffe属性表

    前言 基于深度学习的人脸识别系统,一共用到了5个开源库:OpenCV(计算机视觉库).Caffe(深度学习库).Dlib(机器学习库).libfacedetection(人脸检测库).cudnn(gp ...

  6. GO语言学习(十)Go 语言条件语句

    Go 语言提供了以下几种条件判断语句: 语句 描述 if 语句 if 语句 由一个布尔表达式后紧跟一个或多个语句组成. if...else 语句 if 语句 后可以使用可选的 else 语句, els ...

  7. 程序员的困境 - R中国用户组-炼数成金

    原文:http://www.oschina.net/news/43389/the-plight-of-programmer 在大型公司中不能腐蚀自己的学习能力和时间能力. 最近我为一个内核程序员的职位 ...

  8. JSF教程(11)——生命周期之Invoke Application Phase

    在这个阶段JSF实现将处理不论什么应用界别的事件,比如表单的提交或者链接点击后跳转到还有一个页面. 这时假设应用须要重定向不同 的web应用字眼或者产生一个资源其并不喊不论什么的JSF组件,那么就调用 ...

  9. 让自己的软件实现拖拽打开文件(覆盖WM_DROPFILES,使用DragQueryFile,DragFinish API函数)

    作者: 帅宏军 //声明 protected    procedure WMDROPFILES(var Msg : TMessage); message WM_DROPFILES; --------- ...

  10. 大战C100K之-Linux内核调优篇--转载

    原文地址:http://joyexpr.com/2013/11/22/c100k-4-kernel-tuning/ 早期的系统,系统资源包括CPU.内存等都是非常有限的,系统为了保持公平,默认要限制进 ...