zoukankan      html  css  js  c++  java
  • UVa 1453

    旋转卡壳求凸包直径。

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

      1 #include <cstdio>
      2 #include <cmath>
      3 #include <algorithm>
      4 
      5 using namespace std;
      6 
      7 const int MAXN = 100022 << 2;
      8 
      9 struct Point
     10 {
     11     int x, y;
     12     Point( int x = 0, int y = 0 ):x(x), y(y) { }
     13 };
     14 
     15 typedef Point Vector;
     16 
     17 Vector operator+( Vector A, Vector B )       //向量加
     18 {
     19     return Vector( A.x + B.x, A.y + B.y );
     20 }
     21 
     22 Vector operator-( Vector A, Vector B )       //向量减
     23 {
     24     return Vector( A.x - B.x, A.y - B.y );
     25 }
     26 
     27 Vector operator*( Vector A, double p )      //向量数乘
     28 {
     29     return Vector( A.x * p, A.y * p );
     30 }
     31 
     32 Vector operator/( Vector A, double p )      //向量数除
     33 {
     34     return Vector( A.x / p, A.y / p );
     35 }
     36 
     37 bool operator<( const Point& A, const Point& B )   //两点比较
     38 {
     39     return A.x < B.x || ( A.x == B.x && A.y < B.y );
     40 }
     41 
     42 double Cross( Vector A, Vector B )   //向量叉积
     43 {
     44     return A.x * B.y - A.y * B.x;
     45 }
     46 
     47 int ConvexHull( Point *p, int n, Point *ch )
     48 {
     49     sort( p, p + n );
     50     int m = 0;
     51     for ( int i = 0; i < n; ++i )
     52     {
     53         while ( m > 1 && Cross( ch[m - 1] - ch[m - 2], p[i] - ch[m - 2] ) <= 0 ) --m;
     54         ch[m++] = p[i];
     55     }
     56 
     57     int k = m;
     58     for ( int i = n - 2; i >= 0; --i )
     59     {
     60         while ( m > k && Cross( ch[m - 1] - ch[m - 2], p[i] - ch[m - 2] ) <= 0 ) --m;
     61         ch[m++] = p[i];
     62     }
     63 
     64     if ( n > 1 ) --m;
     65     return m;
     66 }
     67 
     68 int dist( Point a, Point b )
     69 {
     70     return (a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y);
     71 }
     72 
     73 int RotatingCalipers( Point *ch, int n )
     74 {
     75     int q = 1;
     76     int ans = 0;
     77     for ( int i = 0; i < n; ++i )
     78     {
     79         while ( Cross( ch[i + 1] - ch[i], ch[q + 1] - ch[i] ) > Cross( ch[i + 1] - ch[i], ch[q] - ch[i] ) )
     80             q = ( q + 1 ) % n;
     81         ans = max( ans, max( dist( ch[i], ch[q] ), dist( ch[i + 1], ch[q + 1] ) ) );
     82     }
     83     return ans;
     84 }
     85 
     86 Point read_Point( int x, int y )
     87 {
     88     return Point( x, y );
     89 }
     90 
     91 Point P[MAXN], ch[MAXN];
     92 
     93 int main()
     94 {
     95     int T;
     96     scanf( "%d", &T );
     97     while ( T-- )
     98     {
     99         int N, cnt = 0;
    100         scanf( "%d", &N );
    101         for ( int i = 0; i < N; ++i )
    102         {
    103             int x, y, len;
    104             scanf( "%d%d%d", &x, &y, &len );
    105             P[cnt++] = read_Point( x, y );
    106             P[cnt++] = read_Point( x + len, y );
    107             P[cnt++] = read_Point( x, y + len );
    108             P[cnt++] = read_Point( x + len, y + len );
    109         }
    110 
    111         int m = ConvexHull( P, cnt, ch );
    112         printf("%d
    ", RotatingCalipers( ch, m ) );
    113     }
    114     return 0;
    115 }
  • 相关阅读:
    Leecode刷题之旅-C语言/python-383赎金信
    Leecode刷题之旅-C语言/python-349两整数之和
    Cesium 实现粒子效果贴地(伪)
    CesiumJS 添加会动的GIF
    sleep 和 wait 区别
    Git常用命令
    java 泛型
    java 设计模式 ---- 单例模式
    java 设计模式 ---- 工场模式
    java 线程池 ---- newCachedThreadPool()
  • 原文地址:https://www.cnblogs.com/GBRgbr/p/3141323.html
Copyright © 2011-2022 走看看