zoukankan      html  css  js  c++  java
  • HDU1432+几何

    题意:给N个点,求最多有多少个点在同一直线上

    方法一:求出所有能形成的直线,两两比较,统计最多有多少条重合。

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<string.h>
      4 #include<algorithm>
      5 #include<set>
      6 #include<iostream>
      7 using namespace std;
      8 typedef long long int64;
      9 const int64 maxn = 701;
     10 const int64 maxm = 490005;
     11 const int64 inf = -123456789;
     12 struct Point64{
     13     int x,y;
     14 }pnt[ maxn ];
     15 struct Node{
     16     int64 a,b,c;
     17     int x,y;
     18 }tp[ maxm ];
     19 
     20 bool s[ maxn ];
     21 //set<int>s;
     22 
     23 void init( int n ){
     24     for( int i=0;i<n;i++ )
     25         s[ i ] = false;
     26 }
     27 
     28 int64 cmp( Node p1,Node p2 ){
     29     if( p1.a!=p2.a ) return p1.a<p2.a;
     30     else if( p1.b!=p2.b ) return p1.b<p2.b;
     31     else return p1.c<p2.c;
     32 }
     33 
     34 int64 gcd( int64 a,int64 b ){
     35     int64 r;
     36     while( b ){
     37         r = a%b;
     38         a = b;
     39         b = r;
     40     }
     41     return a;
     42 }
     43 
     44 void solve_gcd( int id ){
     45     int64 Gcd = gcd( tp[ id ].a,tp[ id ].b );
     46     Gcd = gcd( tp[ id ].c,Gcd );
     47     tp[ id ].a /= Gcd;
     48     tp[ id ].b /= Gcd;
     49     tp[ id ].c /= Gcd;
     50 }    
     51 
     52 int main(){
     53     int n;
     54     while( scanf("%d",&n)!=EOF ){
     55         for( int64 i=0;i<n;i++ ){
     56             cin>>pnt[i].x>>pnt[i].y;
     57             //scanf("%lld%lld",&pnt[i].x,&pnt[i].y);
     58             s[ i ] = false;
     59         }
     60     if( n==1||n==2 ){
     61         printf("%d
    ",n);
     62         continue;
     63     } 
     64         int cnt = 0;
     65         for( int i=0;i<n;i++ ){
     66             for( int j=i+1;j<n;j++ ){
     67                 tp[ cnt ].a = pnt[ i ].y - pnt[ j ].y;
     68                 tp[ cnt ].b = pnt[ j ].x - pnt[ i ].x;
     69                 tp[ cnt ].c = pnt[ i ].x*pnt[ j ].y - pnt[ j ].x*pnt[ i ].y;
     70                 tp[ cnt ].x = i;
     71                 tp[ cnt ].y = j;
     72                 solve_gcd( cnt );
     73                 cnt ++;
     74             }
     75         }
     76         sort( tp,tp+cnt,cmp );
     77         int64 ans = 2;
     78         int64 ans_tp = 0;
     79         int64 prea = inf;
     80         int64 preb = inf;
     81         int64 prec = inf;
     82         for( int i=0;i<cnt;i++ ){
     83             if(( prea!=tp[ i ].a || preb!=tp[ i ].b || prec!=tp[ i ].c )){
     84                 ans_tp = 2;
     85                 prea = tp[ i ].a;
     86                 preb = tp[ i ].b;
     87                 prec = tp[ i ].c;
     88                 memset( s,false,sizeof( s ) );
     89                 s[ tp[i].x ] = true;
     90                 s[ tp[i].y ] = true;
     91             }
     92             else {
     93                 if( s[ tp[i].x ]==false ) {
     94                     ans_tp ++;
     95                     s[ tp[i].x ] = true;
     96                 }
     97                 if( s[ tp[i].y ]==false ) {
     98                     ans_tp ++;
     99                     s[ tp[i].y ] = true;
    100                 }
    101                 ans = max( ans,ans_tp );
    102             }
    103         }
    104         cout<<ans<<endl;
    105         //printf("%lld
    ",ans);
    106     }
    107     return 0;
    108 }
    View Code

     方法二:利用极角来排序 判断

      1 /*
      2 几何
      3 极角排序
      4 */
      5 #include<algorithm>  
      6 #include<iostream>  
      7 #include<string.h>  
      8 #include<stdlib.h>  
      9 #include<stdio.h>  
     10 #include<math.h>  
     11 #include<queue>  
     12 #include<stack>  
     13 #include<map>  
     14 #include<set>  
     15 using namespace std;  
     16 typedef long long int64;  
     17 //typedef __int64 int64;  
     18 typedef pair<int64,int64> PII;  
     19 #define MP(a,b) make_pair((a),(b))   
     20 const int inf = 0x3f3f3f3f;  
     21 const double inf2 = 987654321.0;
     22 const double pi=acos(-1.0);  
     23 const int dx[]={1,-1,0,0};  
     24 const int dy[]={0,0,1,-1};  
     25 const double eps = 1e-8;  
     26 const int maxm = 1005;  
     27 const int maxn = 705;  
     28 
     29 struct Point{
     30     double x,y;
     31 }pnt[ maxn ],pnt2[ maxn ];
     32 Point C;
     33 
     34 double td[ maxn ];
     35 
     36 void initC( double x,double y ){
     37     C.x = x;
     38     C.y = y;
     39 }
     40 
     41 void initP( int n ){    
     42     for( int i=0;i<n;i++ ){
     43         pnt2[ i ].x = pnt[ i ].x - C.x;
     44         pnt2[ i ].y = pnt[ i ].y - C.y;
     45     }
     46     return ;
     47 } 
     48 
     49 void initTD( int n ){
     50     for( int i=0;i<n;i++ ){
     51         td[ i ] = atan2( pnt2[ i ].y,pnt2[ i ].x );
     52         if( td[i]<0 ) td[ i ] += pi;
     53         //printf("%.2lf ",td[i]);
     54     }
     55     //printf("
    ");
     56     sort( td,td+n );
     57     return ;
     58 }
     59 
     60 double dis( Point A,Point B ){
     61     A.x -= C.x,A.y -= C.y;
     62     B.x -= C.x,B.y -= B.y;
     63     return sqrt( (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y) );
     64 }
     65 
     66 double cmp( Point A,Point B ){
     67     double t1 = atan2( A.y-C.y,A.x-C.x );
     68     double t2 = atan2( B.y-C.y,B.x-C.x );
     69     if( t1<0 ){
     70         t1 = t1 + pi*2.0;
     71     }
     72     if( t2<0 ){
     73         t2 = t2 + pi*2.0;
     74     }
     75     if( t1==t2 ){
     76         return fabs( A.x )< fabs( B.x );
     77     }
     78     else{
     79         return t1<t2;
     80     }
     81 }
     82 /*******************************************
     83 绕原点 以x正半轴开始逆时针旋转
     84 角度相同 按距离原点距离比较 
     85 *******************************************/
     86 
     87 int main(){
     88     //freopen("in.txt","r",stdin);
     89     int n ;
     90     while( scanf("%d",&n)!=EOF ){
     91         for( int i=0;i<n;i++ ){
     92             scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
     93         }
     94         int ans = 0;
     95         double f = inf2;
     96         for( int i=0;i<n;i++ ){
     97             initC( pnt[ i ].x,pnt[ i ].y );
     98             initP( n );
     99             initTD( n );
    100             int cnt ;
    101             double pre = inf2;
    102             for( int j=0;j<n;j++ ){
    103                 if( pre!=td[ j ] ){
    104                     pre = td[ j ];
    105                     cnt = 1;
    106                 }
    107                 else {
    108                     cnt ++;
    109                     if( cnt>ans ){
    110                         ans = cnt;
    111                         f = pre;
    112                     }
    113                     //ans = max( ans,cnt+1 );
    114                 }
    115             }
    116         }
    117         if( f!=0 ) ans ++; 
    118         printf("%d
    ",max(2,ans));
    119     }
    120     return 0;
    121 }
    View Code
  • 相关阅读:
    【SQL】SQL Server登录常见问题
    【毒思】纪念我死去的爱情
    【毒思】化蝶双飞
    VS2013常用快捷键你敢不会?
    SSIS 更新变量
    一次SSIS Package的调试经历
    binary 和 varbinary 用法全解
    Execute SQL Task 第二篇:返回结果集
    OLEDB 命令转换组件的用法
    脚本组件的用法
  • 原文地址:https://www.cnblogs.com/xxx0624/p/3413176.html
Copyright © 2011-2022 走看看