一开始以为是个求面积的,后来发现先要求凸包,然后求凸包面积
有模板的话很好写
#include<stdio.h>
#include<string>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=10005;
const double eps = 1e-8;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}//构造函数
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
};
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
//变量申明
Point list[10005];
Point p[10005];
int Stack[MAXN],top;
double CalcArea(Point p[],int n)
{
double res = 0;
for(int i = 0;i < n;i++)
res += (p[i]^p[(i+1)%n])/2;
return fabs(res);
}
bool _cmp(Point p1,Point p2)
{
double tmp = (p1-list[0])^(p2-list[0]);
if(sgn(tmp) > 0)return true;
else if(sgn(tmp) == 0 && sgn(dist(p1,list[0]) - dist(p2,list[0])) <= 0)
return true;
else return false;
}
void Graham(int n)
{
Point p0;
int k = 0,i;
p0 = list[0];
//找最下边的一个点
for(i = 1;i < n;i++)
{
if( (p0.y > list[i].y) || (p0.y == list[i].y && p0.x > list[i].x) )
{
p0 = list[i];
k = i;
}
}
swap(list[k],list[0]);
sort(list+1,list+n,_cmp);
if(n == 1)
{
top = 1;
Stack[0] = 0;
return;
}
if(n == 2)
{
top = 2;
Stack[0] = 0;
Stack[1] = 1;
return ;
}
Stack[0] = 0;
Stack[1] = 1;
top = 2;
for(i = 2;i < n;i++)
{
while(top > 1 && sgn((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0)
top--;
Stack[top++] = i;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
scanf("%d",&n);
int i;
for(i=0;i<n;i++) {
scanf("%lf%lf",&list[i].x,&list[i].y);
}
Graham(n);
for(i=0;i<top;i++) {
p[i].x=list[Stack[i]].x;
p[i].y=list[Stack[i]].y;
}
int res=CalcArea(p,top)/50;
printf("%d\n",res);
return 0;
}