凸包模板题,求出凸包面积,再除以50就可以了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define eps 1e-10
#define N 1005
struct Point
{
double x, y;
}p[N], res[N];
int dcmp(double x)
{
if (fabs(x) < eps)
return 0;
return x>eps?1:-1;
}
double Cross(Point a, Point b, Point c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dist(Point a, Point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}
int cmp(const void *a, const void *b)
{
Point c = *(Point *)a;
Point d = *(Point *)b;
double t = Cross(p[0], c, d);
if (t)
return -t;
return dist(p[0], d)-dist(p[0], c);
}
int graham(int n)
{
int i, j, k, top = 2;
j = 0;
for (i=1; i<n; i++)
{
if ( p[i].y<p[j].y || (p[i].y==p[j].y && p[i].x<p[j].x) )
j = i;
}
if (j)
{
Point temp = p[j];
p[j] = p[0];
p[0] = temp;
}
qsort(p+1, n-1, sizeof(p[0]), cmp);
res[0] = p[0];
res[1] = p[1];
res[2] = p[2];
for (int i=3; i<n; i++)
{
while ( top>1 && dcmp(Cross(res[top-1], res[top], p[i]))<=0 )
--top;
res[++top] = p[i];
}
return top;
}
int main()
{
int n, r, t, area;
while (~scanf("%d", &n))
{
for (int i=0; i<n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
t = graham(n);
area = 0;
for (int i=1; i<t; i++)
area += Cross(res[0], res[i], res[i+1]);
printf("%.f\n", area(Length/100));
}
return 0;
}
本文介绍了一个凸包模板题的解决方案,通过计算凸包面积并除以50来得出结果。使用了Graham扫描算法确定凸包顶点,并计算面积。
1489

被折叠的 条评论
为什么被折叠?



