POJ-3714 Raid 平面最近点对

  题目链接:http://poj.org/problem?id=3714

  分治算法修改该为两个点集的情况就可以了,加一个标记。。。

 //STATUS:C++_AC_2094MS_4880KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Node{
double x,y;
int id,index;
Node(){}
Node(double _x,double _y,int _index):x(_x),y(_y),index(_index){}
}nod[N],temp[N]; int n; double dist(Node &a,Node &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} bool cmpxy(Node a,Node b)
{
return a.x!=b.x?a.x<b.x:a.y<b.y;
} bool cmpy(Node a,Node b)
{
return a.y<b.y;
} pii Closest_Pair(int l,int r)
{
if(l==r || l+==r)return pii(l,r);
double d,d1,d2;
int i,j,k,mid=(l+r)/;
pii pn1=Closest_Pair(l,mid);
pii pn2=Closest_Pair(mid+,r);
if(pn1.first==pn1.second || nod[pn1.first].index==nod[pn1.second].index)
d1=OO;
else d1=dist(nod[pn1.first],nod[pn1.second]);
if(pn2.first==pn2.second || nod[pn2.first].index==nod[pn2.second].index)
d2=OO;
else d2=dist(nod[pn2.first],nod[pn2.second]); pii ret;
d=Min(d1,d2);
ret=d1<d2?pn1:pn2; for(i=l,k=;i<=r;i++){
if(fabs(nod[mid].x-nod[i].x)<=d){
temp[k++]=nod[i];
}
}
sort(temp,temp+k,cmpy);
for(i=;i<k;i++){
for(j=i+;j<k && fabs(temp[j].y-temp[i].y)<d;j++){
if(dist(temp[i],temp[j])<d && (temp[i].index^temp[j].index)){
d=dist(temp[i],temp[j]);
ret=make_pair(temp[i].id,temp[j].id);
}
}
}
return ret;
} void Init()
{
int i,t;
double x,y;
scanf("%d",&t);
n=t<<;
for(i=;i<t;i++){
scanf("%lf%lf",&x,&y);
nod[i]=Node(x,y,);
}
for(;i<n;i++){
scanf("%lf%lf",&x,&y);
nod[i]=Node(x,y,);
}
sort(nod,nod+n,cmpxy);
for(i=;i<n;i++)nod[i].id=i;
} int main(){
// freopen("in.txt","r",stdin);
int T,i,j;
scanf("%d",&T);
while(T--)
{
Init();
pii ans=Closest_Pair(,n-); printf("%.3lf\n",dist(nod[ans.first],nod[ans.second]));
}
return ;
}
上一篇:Linux下部署Symfony2对app/cache和app/logs目录的权限设置


下一篇:css完成下图