Cows - POJ 3348(凸包求面积)

题目大意:利用n棵树当木桩修建牛圈,知道每头牛需要50平的生存空间,求最多能放养多少头牛。

分析:赤裸裸的求凸包然后计算凸包的面积。

代码如下:

-------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std; const int MAXN = 1e4+;
const double EPS = 1e-; int sta[MAXN], top; struct point
{
double x, y; point(double x=, double y=):x(x), y(y){}
point operator - (const point &t)const{
return point(x-t.x, y-t.y);
}
double operator *(const point &t)const{
return x*t.x + y*t.y;
}
double operator ^(const point &t)const{
return x*t.y - y*t.x;
}
}p[MAXN];
int Sign(double t)
{
if(t > EPS)return ;
if(fabs(t) < EPS)return ;
return -;
}
double Dist(point a, point b)
{
return sqrt((a-b)*(a-b));
}
bool cmp(point a, point b)
{
int t = Sign((a-p[])^(b-p[])); if(t == )
return Dist(a, p[]) < Dist(b, p[]);
return t > ;
}
void Graham(int N)
{
int k=; for(int i=; i<N; i++)
{
if(p[k].y>p[i].y || (Sign(p[k].y-p[i].y)== && p[k].x > p[i].x))
k = i;
}
swap(p[], p[k]);
sort(p+, p+N, cmp); sta[]=, sta[]=, top=; if(N < )
{
top = N-;
return ;
} for(int i=; i<N; i++)
{
while(top> && Sign((p[i]-p[sta[top]])^(p[sta[top-]]-p[sta[top]])) <= )
top--;
sta[++top] = i;
}
}
int main()
{
int N; while(scanf("%d", &N) != EOF && N)
{
for(int i=; i<N; i++)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
} Graham(N); double area=; for(int i=; i<top; i++)
{
double a = Dist( p[ sta[] ], p[ sta[i] ] );
double b = Dist( p[ sta[] ], p[ sta[i+] ] );
double c = Dist( p[ sta[i] ], p[ sta[i+] ] );
double q = (a+b+c) / ; area += sqrt(q*(q-a)*(q-b)*(q-c));
} printf("%d\n", (int)(area/50.0));
} return ;
}
上一篇:3. Longest Substring Without Repeating Characters (ASCII码128个,建立哈西表)


下一篇:POJ 3348 Cows 凸包 求面积