鏈接:https://cn.vjudge.net/problem/POJ-3348
題意:給出n個點。求把這n個點圍起來的凸多邊形的面積,然後除以50。
思路:凸包裸題,凸包不嚴格的說就是把所有點圍起來的凸多邊形。怎麼求呢?按最左下的點進行極角排序,然後把凸包中的點放進棧中。每次要放點時,判斷一下是不是向左轉,若向右轉則把棧頂出棧。注意判斷向左時,重合的情況。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1e4+10;
const double eps = 1e-8;
int sgn(double x)
{
if(fabs(x)<eps) return 0;
else if(x<0) return -1;
else return 1;
}
struct Point
{
int x,y;
Point(){}
Point(int x,int y):x(x),y(y){}
Point operator -(const Point& b)const//相減
{
return Point(x-b.x,y-b.y);
}
int operator ^(const Point& b)const//叉乘
{
return x*b.y-y*b.x;
}
}ps[N],st[N];
int n,pos,top;
double x;
bool cmp(const Point& a,const Point& b)
{
x=(a-ps[1])^(b-ps[1]);
if(x==0)
{
if(a.y==b.y)
return a.x<b.x;
else return a.y<b.y;
}
else if(x>0) return 1;
else return 0;
}
bool check(Point a,Point b,Point c)
{
x=(b-a)^(c-a);
return x<=0;
}
double area()
{
++top;
if(top<3) return 0;
int ans=st[0].y*(st[top-1].x-st[1].x);
for(int i=1;i<top;i++)
{
ans+=st[i].y*(st[i-1].x-st[(i+1)%top].x);
}
return abs(ans/100);
}
int main(void)
{
while(~scanf("%d",&n))
{
pos=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&ps[i].x,&ps[i].y);
if(ps[i].y<ps[pos].y || (ps[i].y==ps[pos].y&&ps[i].x<ps[pos].x))
pos=i;
}
if(n<3)
{
puts("0");
continue;
}
swap(ps[1],ps[pos]);
sort(ps+2,ps+n+1,cmp);
top=-1;
st[++top]=ps[1];
st[++top]=ps[2];
for(int i=3;i<=n;i++)
{
while(top>1&&check(st[top-1],st[top],ps[i]))
top--;
st[++top]=ps[i];
}
printf("%d\n",(int)area());
}
return 0;
}