您的位置:首页 > 其它

强大的旋转卡壳 POJ 2187 最远点对 POJ 2079点集中面积最大的三角形

2013-10-26 18:46 681 查看
强大的旋转卡壳

POJ 2187 最远点对    

POJ 2079点集中面积最大的三角形

此题,最开始我用N^2*logn的旋转卡壳,超时。于是在网上找到了个也是N^2*logn减枝3秒少一点刚好过掉。但是强大的是有个用O(N)的旋转卡壳过掉了。虽然看得懂,但是不能证明出它的正确性。但是旋转卡壳的强大与灵活太让我震撼了。





Source Code

Problem: 2187 User: liu696639
Memory: 1444K Time: 110MS
Language: G++ Result: Accepted
//POJ 2187

#include<algorithm>

#include<iostream>

#include<stdio.h>

#include<string.h>

#include<cmath>

using namespace std; 

const double EP =1E-10; 

struct POINT 



 int x;  int y; 

 POINT(int a=0, int b=0) { x=a; y=b;} //constructor 

};

double multiply(POINT sp,POINT ep,POINT op) 



 return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y)); 



int dist_2(POINT p1,POINT p2)                // 返回两点之间欧氏距离的平方 



 return(   (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)   ); 

}

bool cm(const POINT &a,const POINT &b)

{

     if( a.y<b.y||(a.y==b.y && a.x<b.x))

     return true;

     return false;

}

void Graham_scan(POINT PointSet[],POINT ch[],int n,int &len)

{

    sort(PointSet, PointSet+n,cm);

    ch[0]=PointSet[0];

    ch[1]=PointSet[1];

    int top=1;

    for(int i=2;i<n;i++)

    {

        while(top>0&&multiply(ch[top],PointSet[i],ch[top-1])<=0)

        {

            top--;

        }

        ch[++top]=PointSet[i];

    }

    int tmp=top;

    for(int i=n-2;i>=0;i--)

    {

                while(top>tmp&&multiply(ch[top],PointSet[i],ch[top-1])<=0)

                    top--;

                ch[++top]=PointSet[i];

    }

    len=top;    

}

int max(int a,int b)

{      

       if(a>b)return a;

       else return b;

}

int rotating_calipers(POINT ch[],int chsz)

{

    int pos=1,ans=0;     

    ch[chsz]=ch[0];     

    for(int i=0;i<chsz;i++)     

    {         

              while(multiply(ch[i+1],ch[pos],ch[i])<multiply(ch[i+1],ch[pos+1],ch[i]))            

                   pos=(pos+1)%chsz;         

              ans=max(ans,max(dist_2(ch[i],ch[pos]),dist_2(ch[i+1],ch[pos+1])));     

    }     

    return ans;

}//*/

int main()



    POINT ch[50005];POINT PointSet[50005];

    int n;

    while(scanf("%d",&n)!=EOF)

    {

            for(int i=0;i<n;i++)

            {

                    scanf("%d%d",&PointSet[i].x,&PointSet[i].y);

            }

            if(n==2)

            {

                    printf("%d\n",dist_2(PointSet[1],PointSet[0]));

            }

            else

            {

                    int len;

                    Graham_scan(PointSet,ch,n,len);

                    printf("%d\n",rotating_calipers(ch,len));

            }

    }    

}

Source Code

Problem: 2079 User: liu696639
Memory: 2236K Time: 94MS
Language: G++ Result: Accepted
#include <queue>

#include <stack>

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

#include <iostream>

#include <limits.h>

#include <string.h>

#include <string>

#include <algorithm>

using namespace std;

const int MAX = 50010;

const double eps = 1e-6;

bool dy(double x,double y) { return x > y + eps;} // x > y 

bool xy(double x,double y) { return x < y - eps;} // x < y 

bool dyd(double x,double y) {  return x > y - eps;} // x >= y 

bool xyd(double x,double y) { return x < y + eps;}  // x <= y 

bool dd(double x,double y)  { return fabs( x - y ) < eps;}  // x == y

struct point{ double x,y;  };

point c[MAX],stk[MAX];

int top;

double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 

{

 return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);

}

double disp2p(point a,point b) 

{

 return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );

}

bool cmp(point a,point b)  // 排序   

{  

    double len = crossProduct(c[0],a,b);  

    if( dd(len,0.0) )  

        return xy(disp2p(c[0],a),disp2p(c[0],b));  

    return xy(len,0.0);  

}

double area_triangle(point a,point b,point c)

{

 return fabs( crossProduct(a,b,c) )/2.0;

}

double max(double x,double y)

{

 return dy(x,y) ? x : y;

}

double RC(point *s,int n)

{

 int p,q,r;

 p = 0; q = 1; r = 2;

 double area = area_triangle(s[p],s[q],s

);
 while(1)
 {
  int pp = p,qq = q,rr = r;
  while( xy(fabs(crossProduct(s[p],s[q],s

)),
    fabs(crossProduct(s[p],s[q],s[(r+1)%n]))))
  {
   area = max(area,area_triangle(s[p],s[q],s[(r+1)%n]));
   r = (r+1)%n;
  }
  while( xy(fabs(crossProduct(s[p],s[q],s

)),
    fabs(crossProduct(s[p],s[(q+1)%n],s

))))
  {
   area = max(area,area_triangle(s[p],s[(q+1)%n],s

));
   q = (q+1)%n;
  }
  while( xy(fabs(crossProduct(s[p],s[q],s

)),
    fabs(crossProduct(s[(p+1)%n],s[q],s

))))
  {
   area = max(area,area_triangle(s[(p+1)%n],s[q],s

));
   p = (p+1)%n;
  }
  if( pp == p && qq == q && rr == r )  // 如果一步都前进不了 
   r = (r+1)%n;
  if( r == 0 ) return area;
 }  
}

double Graham(int n)

{

    int tmp = 0;  

    for(int i=1; i<n; i++)

     if( xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y) )

      tmp = i;

    swap(c[0],c[tmp]);

    sort(c+1,c+n,cmp);

    stk[0] = c[0]; stk[1] = c[1];

    top = 1;

 for(int i=2; i<n; i++)

 {

  while( xy( crossProduct(stk[top],stk[top-1],c[i]), 0.0 ) && top >= 1 )

   top--;

  stk[++top] = c[i];

 }

 return RC(stk,top+1);

}

int main()

{

 int n;

 

 while( ~scanf("%d",&n) && n != -1 )

 {

  for(int i=0; i<n; i++)

   scanf("%lf%lf",&c[i].x,&c[i].y);

  double ans = Graham(n);

  

  printf("%.2lf\n",ans);

 }

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息