UVALive 4728 Squares(旋转卡壳)

  

 Squares

The famous Korean IT company epsfbox{p4728_c.eps} plans to make a digital map of the Earth with help of wireless sensors which spread out in rough terrains. Each sensor sends a geographical data to epsfbox{p4728_c.eps}. But, due to the inaccuracy of the sensing devices equipped in the sensors, epsfbox{p4728_c.eps} only knows a square region in which each geographical data happens. Thus a geographical data can be any point in a square region. You are asked to solve some geometric problem, known as diameter problem, on these undetermined points in the squares.

A diameter for a set of points in the plane is defined as the maximum (Euclidean) distance among pairs of the points in the set. The diameter is used as a measurement to estimate the geographical size of the set. epsfbox{p4728_c.eps} wants you to compute the largest diameter of the points chosen from the squares. In other words, given a set of squares in the plane, you have to choose exactly one point from each square so that the diameter for the chosen points is maximized. The sides of the squares are parallel to X-axis or Y-axis, and the squares may have different sizes, intersect each other, and share the same corners.

For example, if there are six squares as in the figure below, then the largest diameter is defined as the distance between two corner points of squares S1 and S4.

epsfbox{p4728.eps}

Given a set of n squares in the plane, write a program to compute the largest diameter D of the points when a point is chosen from each square, and to output D2, i.e., the squared value of D.

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. The first line of each test case contains an integer, n, the number of squares, where 2$ le$n$ le$100, 000. Each line of the next n lines contains three integers, xy, and w, where (xy) is the coordinate of the left-lower corner of a square and w is the length of a side of the square; 0$ le$xy$ le$10, 000 and 1$ le$w$ le$10, 000.

Output

Your program is to write to standard output. Print exactly one line for each test case. The line should contain the integral value D2, whereD is the largest diameter of the points when a point is chosen from each square.

The following shows sample input and output for two test cases.

Sample Input

2 
3 
0 0 1 
1 0 2 
0 0 1 
6 
2 1 2 
1 4 2 
3 2 3 
4 4 4 
6 5 1 
5 1 3

Sample Output

13 
85

求出矩阵那些点中,最远的两个点。

那么先求出凸包,然后再用旋转卡壳来弄出最大。

刚刚学卡壳的一到题。 

WA在了设置初始对踵点,不可把对踵点设为第一个点,要设成第二个点。

这里我不太懂。

#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[n] = 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
",Rotating_Calipers(ch,m));
}

int main(){
    int _ ; scanf("%d",&_);
    while(_--) Run();
}
View Code
only strive for your goal , can you make your dream come true ?
原文地址:https://www.cnblogs.com/hlmark/p/4150722.html