zoukankan      html  css  js  c++  java
  • HDU1392 凸包

    裸题~~+模板!!!

    View Code
      1  /*
      2  几何 凸包
      3  顺时针!!!
      4  */
      5  #include<stdio.h>
      6  #include<string.h>
      7  #include<stdlib.h>
      8  #include<algorithm>
      9  #include<iostream>
     10  #include<queue>
     11  //#include<map>
     12  #include<math.h>
     13  using namespace std;
     14  typedef long long ll;
     15  //typedef __int64 int64;
     16  const int maxn = 105;
     17  const int inf = 0x7fffffff;
     18  const int pi=acos(-1.0);
     19  struct node{
     20      int x,y;
     21      bool operator <( const node &a ) const {
     22          return y<a.y||(y==a.y&&x<a.x);
     23      }
     24  };
     25 
     26  node pnt[ maxn ],res[ maxn ];
     27  
     28  int cross( node sp,node ep,node op ){
     29      return (sp.x - op.x) * (ep.y - op.y)-(ep.x - op.x) * (sp.y - op.y);
     30  }
     31  /*
     32  ep
     33  |
     34  |
     35  op----sp
     36  ( from sp to ep )
     37  */
     38  
     39  double dis( node a,node b ){
     40      double sum=0;
     41      sum=1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y);
     42      return sqrt( sum );
     43  }//两点之间的距离
     44  
     45  int graham( int n ){
     46      int top=1;
     47      sort( pnt,pnt+n );
     48      if( n==0 ) return 0;
     49      else res[ 0 ]=pnt[ 0 ];
     50      if( n==1 ) return 1;
     51      else res[ 1 ]=pnt[ 1 ];
     52      if( n==2 ) return 2;
     53      else res[ 2 ]=pnt[ 2 ];
     54      
     55      for( int i=2;i<n;i++ ){
     56          while( top&&cross( res[ top-1 ],pnt[ i ],res[ top ] )>=0 )
     57              top--;    
     58          res[ ++top ]=pnt[ i ];
     59      }
     60      
     61      int len=top;
     62      res[ ++top ]=pnt[ n-2 ];
     63      for( int i=n-3;i>=0;i-- ){
     64          while( top!=len&&cross( res[ top-1 ],pnt[ i ],res[ top ] )>=0 )
     65              top--;
     66          res[ ++top ]=pnt[ i ];
     67      }
     68      
     69      return top;//返回res中的点的个数
     70  }
     71      
     72  int main(){
     73      int n;
     74      while( scanf("%d",&n)==1,n ){
     75          for( int i=0;i<n;i++ )
     76              scanf("%d%d",&pnt[ i ].x,&pnt[ i ].y);
     77          if( n==1 )
     78          {
     79              printf("0.00\n");
     80              continue;
     81          }
     82          if( n==2 )
     83          {
     84              printf("%.2lf\n",dis( pnt[0],pnt[1] ) );
     85              continue;
     86          }//注意这两个情况的讨论!!!
     87          int cnt=graham( n );
     88          double ans=0;
     89          for( int i=0;i<cnt;i++ ){
     90              if( i==cnt-1 ){
     91                  ans+=( dis( res[ cnt-1 ],res[ 0 ] ) );
     92              }
     93              else{
     94                  ans+=( dis( res[ i ],res[ i+1 ] ) );
     95              }
     96          }
     97          printf("%.2lf\n",ans);
     98      }
     99      return 0;
    100  }
    keep moving...
  • 相关阅读:
    Java语言编码规范(Java Code Conventions) 转载
    Flex 彻底屏蔽右键 (转载)
    JAVA 读取文件(收集)
    可以运行SWT的精简版JRE 1.4.2_04, 压缩后仅1.3MB[整理]
    转载:多核平台下的JAVA优化
    森林消防智慧预警:火灾监测 Web GIS 可视化平台
    绿色数字园区运维:一屏群集 3D 可视化智慧楼宇
    js删除json key
    学习、应用Web标准一路走来——《重构之美》原创系列文章快速入口。
    中国互联网的鱼死网破新时代。
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2962209.html
Copyright © 2011-2022 走看看