zoukankan      html  css  js  c++  java
  • POJ-3714 Raid 平面最近点对

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

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

      1 //STATUS:C++_AC_2094MS_4880KB
      2 #include <functional>
      3 #include <algorithm>
      4 #include <iostream>
      5 //#include <ext/rope>
      6 #include <fstream>
      7 #include <sstream>
      8 #include <iomanip>
      9 #include <numeric>
     10 #include <cstring>
     11 #include <cassert>
     12 #include <cstdio>
     13 #include <string>
     14 #include <vector>
     15 #include <bitset>
     16 #include <queue>
     17 #include <stack>
     18 #include <cmath>
     19 #include <ctime>
     20 #include <list>
     21 #include <set>
     22 #include <map>
     23 using namespace std;
     24 //#pragma comment(linker,"/STACK:102400000,102400000")
     25 //using namespace __gnu_cxx;
     26 //define
     27 #define pii pair<int,int>
     28 #define mem(a,b) memset(a,b,sizeof(a))
     29 #define lson l,mid,rt<<1
     30 #define rson mid+1,r,rt<<1|1
     31 #define PI acos(-1.0)
     32 //typedef
     33 typedef __int64 LL;
     34 typedef unsigned __int64 ULL;
     35 //const
     36 const int N=200010;
     37 const int INF=0x3f3f3f3f;
     38 const int MOD=10007,STA=8000010;
     39 const LL LNF=1LL<<55;
     40 const double EPS=1e-8;
     41 const double OO=1e30;
     42 const int dx[4]={-1,0,1,0};
     43 const int dy[4]={0,1,0,-1};
     44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     45 //Daily Use ...
     46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
     47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
     48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
     49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
     50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
     51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
     52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
     53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
     54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
     55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
     56 //End
     57 
     58 struct Node{
     59     double x,y;
     60     int id,index;
     61     Node(){}
     62     Node(double _x,double _y,int _index):x(_x),y(_y),index(_index){}
     63 }nod[N],temp[N];
     64 
     65 int n;
     66 
     67 double dist(Node &a,Node &b)
     68 {
     69     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
     70 }
     71 
     72 bool cmpxy(Node a,Node b)
     73 {
     74     return a.x!=b.x?a.x<b.x:a.y<b.y;
     75 }
     76 
     77 bool cmpy(Node a,Node b)
     78 {
     79     return a.y<b.y;
     80 }
     81 
     82 pii Closest_Pair(int l,int r)
     83 {
     84     if(l==r || l+1==r)return pii(l,r);
     85     double d,d1,d2;
     86     int i,j,k,mid=(l+r)/2;
     87     pii pn1=Closest_Pair(l,mid);
     88     pii pn2=Closest_Pair(mid+1,r);
     89     if(pn1.first==pn1.second || nod[pn1.first].index==nod[pn1.second].index)
     90         d1=OO;
     91     else d1=dist(nod[pn1.first],nod[pn1.second]);
     92     if(pn2.first==pn2.second || nod[pn2.first].index==nod[pn2.second].index)
     93         d2=OO;
     94     else d2=dist(nod[pn2.first],nod[pn2.second]);
     95 
     96     pii ret;
     97     d=Min(d1,d2);
     98     ret=d1<d2?pn1:pn2;
     99 
    100     for(i=l,k=0;i<=r;i++){
    101         if(fabs(nod[mid].x-nod[i].x)<=d){
    102             temp[k++]=nod[i];
    103         }
    104     }
    105     sort(temp,temp+k,cmpy);
    106     for(i=0;i<k;i++){
    107         for(j=i+1;j<k && fabs(temp[j].y-temp[i].y)<d;j++){
    108             if(dist(temp[i],temp[j])<d && (temp[i].index^temp[j].index)){
    109                 d=dist(temp[i],temp[j]);
    110                 ret=make_pair(temp[i].id,temp[j].id);
    111             }
    112         }
    113     }
    114     return ret;
    115 }
    116 
    117 void Init()
    118 {
    119     int i,t;
    120     double x,y;
    121     scanf("%d",&t);
    122     n=t<<1;
    123     for(i=0;i<t;i++){
    124         scanf("%lf%lf",&x,&y);
    125         nod[i]=Node(x,y,0);
    126     }
    127     for(;i<n;i++){
    128         scanf("%lf%lf",&x,&y);
    129         nod[i]=Node(x,y,1);
    130     }
    131     sort(nod,nod+n,cmpxy);
    132     for(i=0;i<n;i++)nod[i].id=i;
    133 }
    134 
    135 int main(){
    136  //   freopen("in.txt","r",stdin);
    137     int T,i,j;
    138     scanf("%d",&T);
    139     while(T--)
    140     {
    141         Init();
    142         pii ans=Closest_Pair(0,n-1);
    143 
    144         printf("%.3lf
    ",dist(nod[ans.first],nod[ans.second]));
    145     }
    146     return 0;
    147 }
  • 相关阅读:
    checkbox的checked事件的javascript使用方法
    JSTL标签API(c)的使用
    radios控件的使用
    验证方法判斷input是否为空
    软件课设Day5
    软件课设Day4
    软件课设Day3
    软件课设Day2
    软件课设Day1
    2019/08/23最新进展
  • 原文地址:https://www.cnblogs.com/zhsl/p/3235774.html
Copyright © 2011-2022 走看看