• HDU-1007 Quoit Design 平面最近点对


      题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007

      简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少。。

      1 //STATUS:G++_AC_1703MS_5004KB
      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=100010;
     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=1e15;
     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 struct Node{  //id为nod排序后的编号值,index为排序前的标号值(随便自己定义)
     58     double x,y;
     59     int id,index;
     60     Node(){}
     61     Node(double _x,double _y,int _index):x(_x),y(_y),index(_index){}
     62 }nod[N],temp[N];
     63 
     64 int n;
     65 
     66 double dist(Node &a,Node &b)
     67 {
     68     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
     69 }
     70 
     71 bool cmpxy(Node a,Node b)  //先按x排序,然后按y排序
     72 {
     73     return a.x!=b.x?a.x<b.x:a.y<b.y;
     74 }
     75 
     76 bool cmpy(Node a,Node b)  //按y排序
     77 {
     78     return a.y<b.y;
     79 }
     80 
     81 pii Closest_Pair(int l,int r)  //返回排序后点的编号
     82 {
     83     if(l==r || l+1==r)return pii(l,r);   //只有一个点或者两个点
     84     double d,d1,d2;
     85     int i,j,k,mid=(l+r)/2;
     86     //左右两边最小距离点的编号
     87     pii pn1=Closest_Pair(l,mid);
     88     pii pn2=Closest_Pair(mid+1,r);
     89     //左右两遍的最小距离
     90     d1=(pn1.first==pn1.second?OO:dist(nod[pn1.first],nod[pn1.second]));
     91     d2=(pn2.first==pn2.second?OO:dist(nod[pn2.first],nod[pn2.second]));
     92     pii ret;
     93     d=Min(d1,d2);
     94     ret=d1<d2?pn1:pn2;
     95     //找出在mid-d,mid+d之间的点
     96     for(i=l,k=0;i<=r;i++){
     97         if(fabs(nod[mid].x-nod[i].x)<=d){
     98             temp[k++]=nod[i];
     99         }
    100     }
    101     //合并两边,求最小距离
    102     sort(temp,temp+k,cmpy);
    103     for(i=0;i<k;i++){
    104         for(j=i+1;j<k && fabs(temp[j].y-temp[i].y)<d;j++){
    105             if(dist(temp[i],temp[j])<d){
    106                 d=dist(temp[i],temp[j]);
    107                 ret=make_pair(temp[i].id,temp[j].id);
    108             }
    109         }
    110     }
    111 
    112     return ret;
    113 }
    114 
    115 void Init()   //初始化点
    116 {
    117     int i;
    118     double x,y;
    119     for(i=0;i<n;i++){
    120         scanf("%lf%lf",&x,&y);
    121         nod[i]=Node(x,y,i);
    122     }
    123     sort(nod,nod+n,cmpxy);
    124     for(i=0;i<n;i++)nod[i].id=i;  //排序后节点编号
    125 }
    126 
    127 int main(){
    128  //   freopen("in.txt","r",stdin);
    129     int T,i,j;
    130     while(~scanf("%d",&n) && n)
    131     {
    132         Init();
    133         pii ans=Closest_Pair(0,n-1);
    134 
    135         printf("%.2lf
    ",dist(nod[ans.first],nod[ans.second])/2);
    136     }
    137     return 0;
    138 }
  • 相关阅读:
    罗辑思维 140 认钱不认人(刚需是扯淡,一切都是稀缺,人生全是选择)——理性永远都是最珍贵的
    程序最多能new多少内存(2G内存里要放程序的5大区,HeapAlloc比new要快多了,而且超过2G的时候会告诉你)
    Qt中使用ActiveX(3篇)
    windows下的socket网络编程(入门级)
    网络数据包发送工具PacketSender中文源码
    avalon.js实现一个简易日历
    avalonJS入门(一)
    JS中的模块规范(CommonJS,AMD,CMD)
    程序员必看的书
    简单的语音聊天室
  • 原文地址:https://www.cnblogs.com/zhsl/p/3235784.html
Copyright © 2020-2023  润新知