可做求凸包面积的模板
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct Point
{
int x;
int y;
};
int N, d; // 顶点数
Point shape[1000]; // 记录多边形各个顶点
Point bulgeShape[1001]; // (栈)记录凸包中各顶点
int top; // 栈顶
/*
* 返回两点间距离
*/
double Dis( const Point & p1, const Point & p2 )
{
return sqrt( (double)((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)) );
}
/*
* p2为交点, 返回p1在p2的什么方向
*/
int Mul( const Point & p1, const Point & p2, const Point & p3 )
{
return ( p1.x - p2.x ) * ( p3.y - p2.y ) - ( p3.x - p2.x ) * ( p1.y - p2.y );
}
/*
* 返回真表示p1与shape[0]的极角比p2与shape[0]的极角大
*/
bool cmp ( const Point p1, const Point p2 )
{
return Mul( p1, shape[0], p2 ) > 0;
}
/*
* 寻找多边形shape的凸包,将凸包记录在bulgeShape中
*/
void Graham_Scan()
{
/* 寻找多边形的最低点,若有多个,则找最左边的点 */
int i;
int pos = 0;
for( i = 1; i < N; ++ i )
{
if( shape[i].y < shape[pos].y ||
shape[i].y == shape[pos].y && shape[i].x < shape[pos].x )
pos = i;
}
Point temp = shape[0];
shape[0] = shape[pos];
shape[pos] = temp;
/* 对多边形中所有顶点按与shape[0]的极角大小从小到大排序(不考虑极角相等的情况) */
sort( shape + 1, shape + N, cmp );
/* 找凸包 */
top = 0; // 置栈顶为空
bulgeShape[top++] = shape[0];
bulgeShape[top++] = shape[1];
for( i = 2; i < N; ++ i )
{
/* 下面这句判断3点共线 */
if( Mul( shape[i], bulgeShape[top-1], bulgeShape[top-2] ) == 0 && // 三点共线
( (shape[i].x - bulgeShape[top-2].x) * (shape[i].x - bulgeShape[top-1].x) < 0 || // 且新点在另两点之间
(shape[i].y - bulgeShape[top-2].y) * (shape[i].y - bulgeShape[top-1].y) < 0 ) )
{
continue;
}
while( top - 2 >= 0 && Mul( shape[i], bulgeShape[top-1], bulgeShape[top-2] ) <= 0 )
{
-- top;
}
bulgeShape[top++] = shape[i];
}
}
int main()
{
freopen("test.txt","r",stdin);
int i;
cin >> N >> d;
for( i = 0; i < N; ++ i )
cin >> shape[i].x >> shape[i].y;
Graham_Scan();
bulgeShape[top] = bulgeShape[0];
double tempSum = 0;
for( i = 0; i < top; ++ i )
{
tempSum += Dis( bulgeShape[i], bulgeShape[i+1] );
}
tempSum += 2 * 3.141592654 * d;
int sum = (int)( tempSum + 0.5 );
cout << sum << endl;
return 0;
}