为什么有一个点总是WA?
查看原帖
为什么有一个点总是WA?
505959
YangJinxi_7_22楼主2023/8/10 17:28
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <iomanip>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const double eps = 1e-9;
struct point{
    double x , y;
    bool operator == ( const point &p )const{
        return fabs(x-p.x) < eps && fabs(y-p.y) < eps;
    }
    bool operator < ( const point &p )const{
        if( fabs( x - p.x ) > eps ) return x < p.x;
        else return y < p.y;
    }
};

struct vec{
    double x , y;
};

vec toVec( point a , point b ){
    vec c;
    c.x = b.x-a.x;
    c.y = b.y-a.y;
    return c;
}

ll dis( point a , point b ){
    return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}

double dot( vec a , vec b ){
    return a.x*b.x + a.y*b.y;
}

double cross( vec a , vec b ){
    return a.x*b.y - a.y*b.x;
}

bool ccw( point a , point b , point c ){
    vec ab = toVec( a , b );
    vec ac = toVec( a , c );
    return cross( ab , ac ) < eps;
}

double trisq( point a , point b , point c ){
    vec ab = toVec( a , b );
    vec ac = toVec( a , c );
    return fabs( cross( ab , ac ) )/2.0;
}

vector<point> CH_Andrew( vector<point> &Pts ){
    int n = Pts.size( ) , k = 0;
    vector<point> H( 2*n );
    sort( Pts.begin( ) , Pts.end( ) );
    for( int i = 0 ; i < n ; i++ ){
        while( k >= 2 && !ccw( H[k-2] , H[k-1] , Pts[i] ) ) --k;
        H[k++] = Pts[i];
    }
    for( int i = n-2 , t = k+1 ; i >= 0 ; i-- ){
        while( k >= t && !ccw( H[k-2] , H[k-1] , Pts[i] ) ) --k;
        H[k++] = Pts[i];
    }
    H.resize( k );
    return H;
}

double diameter( vector<point> &p ){
    int n = p.size( );
    if( n == 3 ) return dis( p[0] , p[1] );
    ll ans = 0;
    int cur = 0;
    for( int i = 0 ; i < n-1 ; i++ ){
        while( trisq( p[cur] , p[i] , p[i+1] ) < trisq( p[(cur+1)%n] , p[i] , p[i+1] ) ) cur = ( cur + 1 )%n;
        ans = max( ans , max( dis( p[i] , p[cur] ) , dis( p[i+1] , p[cur] ) ) );
    }
    return ans;
}

int n;
vector<point> plg;

int main( ) {
    cin >> n;
    point P;
    for( int i = 1 ; i <= n ; i++ ){
        cin >> P.x >> P.y;
        plg.push_back( P );
    }
    auto Hull = CH_Andrew( plg );
    //if( Hull.size( ) > 1 ) Hull.pop_back( );
    ll ans = 0;
    ans = diameter( Hull );
    cout << ans <<endl;
    return 0;
}
2023/8/10 17:28
加载中...