先求出凸包,然后利用凸包求出面积,除以50就是答案
代码:
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int MAXN=10005; struct Point { double x, y; Point() {} Point(double x, double y) { this->x = x; this->y = y; } void read() { scanf("%lf%lf", &x, &y); } } list[MAXN], p[MAXN]; typedef Point Vector; Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); } Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); } double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积 int stack[MAXN], top; int xmult(Point p0, Point p1, Point p2) { return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x); } double dist(Point p1,Point p2) { return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)); } //极角排序函数 , 角度相同则距离小的在前面 bool cmp(Point p1,Point p2) { int tmp= xmult(list[0], p1, p2); if(tmp > 0) return true; else if(tmp == 0 && dist(list[0], p1) < dist(list[0], p2)) return true; else return false; } //输入并把最左下方的点放在list[0]并且进行极角排序 void init(int n) { int i, k; Point p0; scanf("%lf%lf", &list[0].x, &list[0].y); p0.x = list[0].x; p0.y = list[0].y; k = 0; for(i = 1; i < n; i++) { scanf("%lf%lf", &list[i].x, &list[i].y); if((p0.y > list[i].y) || ((p0.y == list[i].y) && (p0.x > list[i].x))) { p0.x = list[i].x; p0.y = list[i].y; k = i; } } list[k] = list[0]; list[0] = p0; sort(list + 1, list + n, cmp); } void graham(int n) { int i; if(n == 1) {top = 0; stack[0] = 0;} if(n == 2) { top = 1; stack[0] = 0; stack[1] = 1; } if(n > 2) { for(i = 0; i <= 1; i++) stack[i] = i; top = 1; for(i = 2; i < n; i++) { while(top > 0 && xmult(list[stack[top - 1]], list[stack[top]], list[i]) <= 0) top--; top++; stack[top]=i; } } for (int i = 0; i <= top; i++) p[i] = list[stack[i]]; } double PolygonArea(Point *p, int n) { double area = 0; for (int i = 1; i < n - 1; i++) area += Cross(p[i] - p[0], p[i + 1] - p[0]); return fabs(area) / 2; } int n; int main() { while (~scanf("%d", &n)) { init(n); graham(n); printf("%d\n", (int)(PolygonArea(p, top +1) / 50.0)); } return 0; }
时间: 2024-11-06 08:37:13