您的位置:首页 > 其它

UVALive 4728 Squares(旋转卡壳)

2014-12-08 11:43 579 查看
  

 Squares

The famous Korean IT company

#include <bits/stdc++.h>
using namespace std;
const int N = 100005 ;
int n , tot ;
struct Point {
int x , y ;
Point(){};
Point(int a , int b ){x=a,y=b;}
bool operator < ( const Point &a ) const {
if( x != a.x )return x < a.x ;
else return y < a.y ;
}
}p[N<<2],ch[N<<2];

inline int Cross( Point a , Point b ) { return a.x*b.y-a.y*b.x ; }
inline int dis( Point a , Point b ) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
Point operator - ( Point a , Point b ) { return Point(a.x-b.x,a.y-b.y); }
int ConvexHull( Point* p , int n , Point* ch ){

int m = 0 ;
sort( p , p + n );
for( int i = 0 ; i < n ; ++i ) {
while( m > 1 && Cross( ch[m-1]-ch[m-2] , p[i]-ch[m-2] ) <= 0 ) m--;
ch[m++] = p[i];
}
int k = m ;
for( int i = n-2 ; i >=0 ; --i ){
while( m > k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0 ) m--;
ch[m++] = p[i];
}
if( n > 1 ) m--;
return m ;
}

int Rotating_Calipers( Point* poly , int n ) {

int j = 1 , ans = 0 ;
poly
= poly[0] ;
for( int i = 0 ; i < n ; ++i ) {
while( fabs( Cross(poly[i+1]-poly[i],poly[j]-poly[i]) ) < fabs( Cross(poly[i+1]-poly[i],poly[j+1]-poly[i]))) j=(j+1)%n ;
ans = max( ans , max( dis(poly[i],poly[j]) , dis(poly[i+1] ,poly[j])));
}
return ans ;
}

void Run() {
int x , y , w ;
scanf("%d",&n);
tot = 0 ;
for( int i = 0 ; i < n ; ++i ) {
scanf("%d%d%d",&x,&y,&w);
p[tot++]=Point(x,y);
p[tot++]=Point(x+w,y);
p[tot++]=Point(x+w,y+w);
p[tot++]=Point(x,y+w);
}
int m = ConvexHull( p , tot , ch );
printf("%d\n",Rotating_Calipers(ch,m));
}

int main(){
int _ ; scanf("%d",&_);
while(_--) Run();
}


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