Study notes stuck rotation

  Calculating geometric rotation stuck content belongs, the plane used for obtaining the farthest point pair.

  First, the farthest point is easy to prove two points must be on the convex hull, for n points in the plane, first find all the points on convex hull using graham scanning method, the complexity of $ O (nlogn) $. Then, assuming the point furthest point of m, n, points m and n to the farthest distance, two set point n on the convex side of the package as l1 and L2, denoted m a straight line l1, l2 distance dis (m, l1) and dis (m, l2), then the point n to edge distance l1 or l2 must be the furthest convex hull, the other convex hull does not point to the presence of an edge distance is greater than its convex hull . I.e., max (dis (m, l1), dis (m, l2))> = dis (p, l), where p is a point on the convex hull, l is an edge on the convex hull. It read a lot of blog are written is easy to prove, I permit last week for two days Leng Shimo card out there can help answer the welcome message, pleaded with very big God to help explain.

  Subsequently, the rotation is the essence of stuck, for the edge l1, if the distance to the farthest point p1, when l2 l1 counterclockwise to the search point, the point farthest distance l2 points p1 p1 or counterclockwise p2, i.e., max (dis (l2, p1), dis (l2, p2))> = dis (l2, p), p is an arbitrary point on the convex hull. Thus, only while the corresponding point in time can also move counterclockwise enumeration counterclockwise side.

  Examples poj2187, this question the data may be random, although many points, but the number of points calculated on the convex hull is very small, in fact the convex hull enumeration distance between any two can be, but the data size limit 4e5 complex of $ O (n ^ {2}) $, the data structure can, all the points are bumps on the package, it snaps off, the rotation is the positive solution stuck.

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int ,int> pii;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rept(i,x,y) for(int i=x;i<=y;i++)
#define per(i,x,y) for(int i=x;i>=y;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
#define mes(a,b) memset(a,b,sizeof a)
const int inf= 0x3f3f3f3f;
const int maxn=5e4+5;

pii point[maxn];//存放点,下标从0开始
double dis(const pii &s1,const pii &s2)
{
    return sqrt((s1.fi-s2.fi)*(s1.fi-s2.fi)+(s1.se-s2.se)*(s1.se-s2.se));
}
ll powdis(const pii &s1,const pii &s2)
{
    return ((ll)s1.fi-s2.fi)*(s1.fi-s2.fi)+(s1.se-s2.se)*(s1.se-s2.se);
}

pii operator -(const pii &s1,const pii &s2)
{
    return mp(s1.fi-s2.fi,s1.se-s2.se);
}
int s[maxn];//
int chaji(const pii &s1,const pii &s2)//差积 
{
    return s1.fi*s2.se-s1.se*s2.fi;
}
bool comp(const pii &s1,const pii &s2)
{
    int x=chaji(s1-point[0],s2-point[0]);
    if( x>0|| (x==0&&fabs(s1.fi-point[0].fi)<fabs(s2.fi-point[0].fi)) ) return 1;
    else return 0;
}

double sqare(const pii &a,const pii &b,const pii &c)
{
    return 0.5*abs( (b.fi-a.fi)*(c.se-a.se)-(c.fi-a.fi)*(b.se-a.se));
}

int main()
{
    :: sync_with_stdio iOS ( to false ); 
    cout.tie ( 0 ); 
    cin.tie ( 0 );
     int n-, cnt = 0 ; // n-number of points, cnt is the number of the convex-hull point 
    CIN >> n-; 
    REP (I, 0 , n-) >> CIN Point [I] .fi >> Point [I] .se;
     IF (n-== 2 ) 
    { 
        COUT << powdis (Point [ 0 ], Point [ . 1 ]) << " \ n- " ;
         return  0 ; 
    } 
    int P = 0 ; 
    REP (I,1,n)
        if( point[i].se<point[p].se||(point[i].se==point[p].se&&point[i].fi<point[p].fi) )
            p=i;
    swap(point[0],point[p]);
    sort(point+1,point+n,comp);
    s[cnt++]=0;
    s[cnt++]=1;
    rep(i,2,n)
    {
        while(cnt>=2&&chaji( point[s[cnt-1]]-point[i],point[s[cnt-2]]-point[i] )>=0 ) cnt--;
        s[cnt++]=i;
    }
    ll ans=0;
    p=2;
    rep(i,0,cnt)
    {
        while(sqare(point[s[i]],point[s[(i+1)%cnt]],point[s[p]])<sqare(point[s[i]],point[s[(i+1)%cnt]],point[s[(p+1)%cnt]]))
            p=(p+1)%cnt;
        ans=max(ans,powdis(point[s[i]],point[s[p]]));
        ans=max(ans,powdis(point[s[(i+1)%cnt]],point[s[p]]));
        ans=max(ans,powdis(point[s[i]],point[s[(p+1)%cnt]]));
        ans=max(ans,powdis(point[s[(i+1)%cnt]],point[s[(p+1)%cnt]]));
    }
    
    cout<<ans<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FZUzyz/p/11570154.html