UVa 1453 - Squares 旋转卡壳求凸包直径

旋转卡壳求凸包直径。

参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html

 #include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; const int MAXN = << ; struct Point
{
int x, y;
Point( int x = , int y = ):x(x), y(y) { }
}; typedef Point Vector; Vector operator+( Vector A, Vector B ) //向量加
{
return Vector( A.x + B.x, A.y + B.y );
} Vector operator-( Vector A, Vector B ) //向量减
{
return Vector( A.x - B.x, A.y - B.y );
} Vector operator*( Vector A, double p ) //向量数乘
{
return Vector( A.x * p, A.y * p );
} Vector operator/( Vector A, double p ) //向量数除
{
return Vector( A.x / p, A.y / p );
} bool operator<( const Point& A, const Point& B ) //两点比较
{
return A.x < B.x || ( A.x == B.x && A.y < B.y );
} double Cross( Vector A, Vector B ) //向量叉积
{
return A.x * B.y - A.y * B.x;
} int ConvexHull( Point *p, int n, Point *ch )
{
sort( p, p + n );
int m = ;
for ( int i = ; i < n; ++i )
{
while ( m > && Cross( ch[m - ] - ch[m - ], p[i] - ch[m - ] ) <= ) --m;
ch[m++] = p[i];
} int k = m;
for ( int i = n - ; i >= ; --i )
{
while ( m > k && Cross( ch[m - ] - ch[m - ], p[i] - ch[m - ] ) <= ) --m;
ch[m++] = p[i];
} if ( n > ) --m;
return m;
} int dist( Point a, Point b )
{
return (a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y);
} int RotatingCalipers( Point *ch, int n )
{
int q = ;
int ans = ;
for ( int i = ; i < n; ++i )
{
while ( Cross( ch[i + ] - ch[i], ch[q + ] - ch[i] ) > Cross( ch[i + ] - ch[i], ch[q] - ch[i] ) )
q = ( q + ) % n;
ans = max( ans, max( dist( ch[i], ch[q] ), dist( ch[i + ], ch[q + ] ) ) );
}
return ans;
} Point read_Point( int x, int y )
{
return Point( x, y );
} Point P[MAXN], ch[MAXN]; int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
int N, cnt = ;
scanf( "%d", &N );
for ( int i = ; i < N; ++i )
{
int x, y, len;
scanf( "%d%d%d", &x, &y, &len );
P[cnt++] = read_Point( x, y );
P[cnt++] = read_Point( x + len, y );
P[cnt++] = read_Point( x, y + len );
P[cnt++] = read_Point( x + len, y + len );
} int m = ConvexHull( P, cnt, ch );
printf("%d\n", RotatingCalipers( ch, m ) );
}
return ;
}
上一篇:smarty练习:考试系统


下一篇:SpringBoot 定时任务升级篇(动态修改cron参数)