Typical convex bag question: Calculate the perimeter of the peripheral wall
C ++ code
# Include <iostream>
# Include <fstream>
# Include <algorithm>
# Include <string>
# Include <set>
// # Include <map>
# Include <queue>
# Include <utility>
# Include <stack>
# Include <list>
# Include <vector>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <cmath>
# Include <ctime>
# Include <ctype. h>
Using namespace std;
# Define PI 3.14159265
Struct point {
Double x, y, angel;
} P [1, 1005], ch [2, 1005];
Double dist (point a, point B)
{
Return sqrt (a. x-b.x) * (a. x-b.x) + (a. y-b.y) * (a. y-b.y ));
}
Double multi (point a, point B, point c)
{
Double x1, y1, x2, y2;
X1 = B. x-a. x;
Y1 = B. y-a. y;
X2 = c. x-B. x;
Y2 = c. y-B. y;
Return x1 * y2-x2 * y1;
}
Bool cmp (point a, point B)
{
If (a. y = B. y)
Return a. x <B. x;
Return a. y <B. y;
}
Bool cmp2 (point a, point B)
{
If (a. angel = B. angel)
{
If (a. x = B. x)
Return a. y> B. y;
Return a. x> B. x;
}
Return a. angel <B. angel;
}
Int main ()
{
Int n, I, top, t;
Double r, len;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d % lf", & n, & r );
For (I = 0; I <n; I ++)
Scanf ("% lf", & p [I]. x, & p [I]. y );
Sort (p, p + n, cmp); // locate p [0] in the lower left corner.
// Locate the polar angle relative to p [0] and sort the vertices except p [0] According to the Polar Angle
For (I = 1; I <n; I ++)
P [I]. angel = atan2 (p [I]. y-p [0]. y, p [I]. x-p [0]. x );
Sort (p + 1, p + n, cmp2 );
// Graham_Scan Algorithm
Ch [0] = p [0], ch [1] = p [1], ch [2] = p [2];
Top = 3;
For (I = 3; I <n; I ++)
{
While (top> 2 & multi (ch [top-2], ch [top-1], p [I]) <= 0)
Top --;
Ch [top ++] = p [I];
}
// Calculate the perimeter
Len = dist (ch [0], ch [top-1]);
For (I = 1; I <top; I ++)
Len + = dist (ch [I], ch [I-1]);
Len + = 2 * PI * r; // Add an arc to the circle!
Printf ("%. 0lf \ n", len );
If (t)
Printf ("\ n ");
}
Return 0;
}