博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3714 Raid(平面最近点对)
阅读量:7221 次
发布时间:2019-06-29

本文共 5137 字,大约阅读时间需要 17 分钟。

Time Limit: 5000MS   Memory Limit: 65536K

 

Total Submissions: 7473
  Accepted: 2221

Description

After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.

The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

Input

The first line is a integer T representing the number of test cases. Each test case begins with an integer N (1 ≤ N ≤ 100000). The next N lines describe the positions of the stations. Each line consists of two i

ntegers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station. The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.  

Output

For each test case output the minimum distance with precision of three decimal placed in a separate line.

Sample Input

240 00 11 01 12 22 33 23 340 00 00 00 00 00 00 00 0

Sample Output

1.4140.000

Source

, Dagger
 
 
 
 
 
 
最近点对,采用分治方法。过程:

1对原数组依据x左标从小到大排序。

2二分数组,左边求出最小值,右边求出最小值,我们求最小的。

3找出对于左右两边的可能小于当前最小值的最近点对,更新最小值。
 
本题很水,直接套模板就秒过!!!

这题目需要区分一下点,让我们求的是闪兵到任意一个核电站的最短距离,加一个标志就可以了。(若是同一类的点就规定距离为oo!!)
 
1 #include 
2 #include
3 #include
4 #include
5 #include
6 7 #define DX(x) ((x) * (x)) 8 using namespace std; 9 10 11 const int MAX = 200000 + 10; 12 const double INF = 10e100; 13 struct Point 14 { 15 double x, y; 16 int index,flag;//flag分类!! 17 }A[MAX], B[MAX], C[MAX]; 18 19 bool compX(const Point& a, const Point& b) 20 { 21 if(a.x == b.x) 22 return a.y < b.y; 23 return a.x < b.x; 24 } 25 bool compY(const Point& a, const Point& b) 26 { 27 if(a.y == b.y) 28 return a.x < b.x; 29 return a.y < b.y; 30 } 31 32 inline double getMin(double a, double b) 33 { 34 return a < b ? a : b; 35 } 36 37 double getDist(Point& a, Point& b) 38 { 39 if(a.flag==b.flag)return INF;//同类规定距离为oo!! 40 return sqrt(DX(a.x - b.x) + DX(a.y - b.y)); 41 } 42 43 void merge(Point p[], Point q[], int s, int m, int t) 44 { 45 int i, j, k; 46 for(i = s, j = m + 1, k = s; i <= m && j <=t;) 47 { 48 if(q[i].y > q[j].y) 49 p[k++] = q[j], j++; 50 else 51 p[k++] = q[i], i++; 52 } 53 while(i <= m) 54 p[k++] = q[i++]; 55 while(j <= t) 56 p[k++] = q[j++]; 57 } 58 59 double closest(Point a[], Point b[], Point c[], int p, int q) 60 { 61 if(q - p == 1) 62 return getDist(a[p], a[q]); 63 if(q - p == 2) 64 { 65 double x1 = getDist(a[p], a[q]); 66 double x2 = getDist(a[p + 1], a[q]); 67 double x3 = getDist(a[p], a[p + 1]); 68 return getMin(x1, getMin(x2, x3)); 69 } 70 int i, j, k, m = (p + q) / 2; 71 double d1, d2; 72 for(i = p, j = p, k = m + 1; i <= q; ++i) 73 { 74 if(b[i].index <= m) 75 c[j++] = b[i]; 76 else 77 c[k++] = b[i]; 78 } 79 d1 = closest(a, c, b, p, m); 80 d2 = closest(a, c, b, m + 1, q); 81 double dm = getMin(d1, d2); 82 merge(b, c, p, m, q); 83 for(i = p, k = p; i <= q; ++i) 84 { 85 if(fabs(b[i].x - b[m].x) < dm) 86 c[k++] = b[i]; 87 } 88 for(i = p; i < k; ++i) 89 for(j = i + 1; j < k && c[j].y - c[i].y < dm; ++j) 90 { 91 double temp = getDist(c[i], c[j]); 92 if(temp < dm) 93 dm = temp; 94 } 95 return dm; 96 } 97 int main() 98 { 99 //freopen("in.txt", "r", stdin);100 int T,n;101 scanf("%d",&T);102 while( T--)103 {104 scanf("%d", &n);105 for(int i = 0; i < n; ++i)106 scanf("%lf%lf", &A[i].x, &A[i].y),A[i].flag=0;107 for(int i = 0; i < n; ++i)108 scanf("%lf%lf", &A[i+n].x, &A[i+n].y),A[i+n].flag=1;109 sort(A, A + 2*n, compX);110 for(int i = 0; i < 2*n; ++i)111 A[i].index = i;112 memcpy(B, A, 2*n * sizeof(B[0]));113 sort(B, B + 2*n, compY);114 double d = closest(A, B, C, 0, 2*n - 1);115 printf("%.3f\n", d);116 }117 return 0;118 }
View Code
 

转载于:https://www.cnblogs.com/skykill/p/3234540.html

你可能感兴趣的文章
邱怡轩:R中大规模矩阵的SVD与矩阵补全
查看>>
C++ Primer习题集(第5版)
查看>>
centos7 mysql 5.7 yum安装
查看>>
JSOUP简单应用
查看>>
Mysql,SqlServer,Oracle主键自动增长的设置
查看>>
开源 java CMS - FreeCMS2.3会员登录
查看>>
malloc(0)的返回值
查看>>
析构方法、克隆对象
查看>>
Python字符编码详解
查看>>
Android开发 Firebase动态链接打开APP
查看>>
基于 HTML5 Canvas 的 3D 模型贴图问题
查看>>
让技术不要成为“背锅侠”!
查看>>
dubbo源码分析系列——dubbo的SPI机制源码分析
查看>>
表格单元格td设置宽度无效的解决办法
查看>>
防止视频资源被下载
查看>>
都是并发惹的祸
查看>>
eclipse实现JavaWeb项目 增量打包
查看>>
面试题系列一之 程序生命周期
查看>>
设计模式——观察者模式:气象监测应用
查看>>
NSUserDefaults简介及如何使用 NSUserDefaults 存储自定义对象
查看>>