- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
-
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?
- 输入
-
The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 1000).
The next N lines describe the positions of the stations. Each line consists of two integers 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. - 输出
-
For each test case output the minimum distance with precision of three decimal placed in a separate line.
- 样例输入
-
2 4 0 0 0 1 1 0 1 1 2 2 2 3 3 2 3 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 样例输出
-
1.414 0.000
题解:就是一个分治的题目,分成左右和中间三部分,即递归找左右最近点,终止条件为l==r,记录左右区间中得到的ans值,再选出x轴坐标离中点在ans之间的一些点,比较这些点之间的距离与ans的大小关系。
//#include<bits/stdc++.h> #include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(a,b,c) make_pair(a,make_pair(b,c)) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<int,pair<int,int> >pii; typedef long long ll; const ll mod = 1000000007; const int INF = 0x3f3f3f3f; ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } const int maxn=200010; int n; double ab(double x) { return x>0?x:-x; } struct node { double x,y; bool BL; } a[maxn],temp[maxn]; bool com_x(const node &a,const node &b) { return a.x<b.x; }//按x排序以分治 bool com_y(const node &a,const node &b) { return a.y<b.y; }//按y排序缩小范围 double min(double x,double y) { if(x<y)return x; else return y; } double dis(node p,node q) { if (p.BL==q.BL) return INF;//同属性距离无穷 return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));//不同属性计算直线距离 } double CP(int l,int r) {//closest_pair if (l==r) return INF; int m=(l+r)/2,cnt=0; double ans=min(CP(l,m),CP(m+1,r)); rep(i,l,r) if (abs(a[i].x-a[m].x)<=ans) temp[++cnt]=a[i]; sort(temp+1,temp+cnt+1,com_y); rep(i,1,cnt) rep(j,i+1,cnt){ if (temp[j].y-temp[i].y>ans) break; ans=min(ans,dis(temp[i],temp[j])); } return ans; } int main() { int t; scanf("%d",&t); while (t--) { scanf("%d",&n); rep(i,1,n) { scanf("%lf%lf",&a[i].x,&a[i].y); a[i].BL=0; } rep(i,1,n) { scanf("%lf%lf",&a[i+n].x,&a[i+n].y); a[i+n].BL=1; } sort(a+1,a+2*n+1,com_x); printf("%.3f\n",CP(1,2*n)); } }