zoukankan      html  css  js  c++  java
  • POJ3714+最近点对

    特判标记即可

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #include<math.h>
     5 #include<algorithm>
     6 using namespace std;
     7 const double eps = 1e-8;
     8 const double inf = 9999999999.0;
     9 const int maxn =  100005;
    10 struct Point{
    11     double x,y;
    12     int flag;
    13 };
    14 Point pnt[ maxn<<1 ],temp[ maxn<<1 ];
    15 double dis( Point a,Point b ){
    16     return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
    17 }
    18 int cmpxy( Point a,Point b ){
    19     if( a.x!=b.x )
    20         return a.x<b.x;
    21     else
    22         return a.y<b.y;
    23 }
    24 int cmpx( Point a,Point b ){
    25     return a.x<b.x;
    26 }
    27 int cmpy( Point a,Point b ){
    28     return a.y<b.y;
    29 }
    30 double solve( int L,int R ){
    31     if( L==R )
    32         return inf;
    33     if( L+1==R ){
    34         if( pnt[L].flag==pnt[R].flag )
    35             return inf;
    36         else
    37             return dis( pnt[L],pnt[R] );
    38     }
    39     int mid = (L+R)/2;
    40     double res,Ldis,Rdis;
    41     Ldis = solve( L,mid );
    42     Rdis = solve( mid+1,R );
    43     res = min( Ldis,Rdis );
    44     int cnt = 0;
    45     for( int i=L;i<=R;i++ ){
    46         if( fabs(pnt[i].x-pnt[mid].x)<=res ){
    47             temp[cnt++] = pnt[i];
    48         }
    49     }
    50     sort( temp,temp+cnt,cmpy );
    51     for( int i=0;i<cnt;i++ ){
    52         for( int j=i+1;j<cnt;j++ ){
    53             if( fabs( pnt[i].y-pnt[j].y )>res ) break;
    54             if( pnt[i].flag==pnt[j].flag ) continue;
    55             res = min( res,dis(pnt[i],pnt[j]) );
    56         }
    57     }
    58     return res;
    59 }
    60 
    61 int main(){
    62     int ca;
    63     scanf("%d",&ca);
    64     while( ca-- ){
    65         int n;
    66         scanf("%d",&n);
    67         for( int i=0;i<n;i++ ){
    68             scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
    69             pnt[i].flag = 1;
    70         }
    71         for( int i=n;i<2*n;i++ ){
    72             scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
    73             pnt[i].flag = 2;
    74         }
    75         sort( pnt,pnt+2*n,cmpxy );
    76         double Ans = solve( 0,2*n-1 );
    77         printf("%.3lf
    ",Ans);
    78     }
    79     return 0;
    80 }
    View Code
    keep moving...
  • 相关阅读:
    C#学习-字段
    C#学习-静态
    C#学习-类的成员
    C#学习-面向对象语言都有类
    必须知道的 Python 专属骚技巧 25 例
    Python3读取、写入、追加写入Excel文件
    python写入excel数据xlwt模块
    Spring Boot 集成 Swagger 1
    Spring Boot 中的全局异常处理
    Java 8 开发
  • 原文地址:https://www.cnblogs.com/xxx0624/p/3218728.html
Copyright © 2011-2022 走看看