swust 最小凸包的面积

来源:互联网 发布:淘宝手机端主图尺寸 编辑:程序博客网 时间:2024/05/12 12:42

http://acm.swust.edu.cn/problem/0249/


求凸包的最小的面积有一个公式:




知道公式,直接裸过;


#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;const int maxn = 1000 + 10;#define INF 0x3f3f3f3f#define eps 10e-8//const int maxn = 1000 + 10;int n,m,k;struct Node{double x,y;double angles;}a[maxn];double distances(Node x,Node y){return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y));}double angle(Node s,Node x){return acos((x.x - s.x) / sqrt((x.x - s.x) * (x.x - s.x) + (x.y - s.y) * (x.y - s.y)));}Node s;bool cmp(Node x,Node y){if(fabs(x.angles - y.angles) < eps)return fabs(x.x - s.x) < fabs(y.x - s.x);return x.angles < y.angles;}double across_multi(Node p1,Node p2,Node p3){return (p1.x - p2.x)* (p3.y - p2.y) - (p1.y - p2.y) *(p3.x - p2.x);}double gramham(){s = a[1];for(int i = 2; i <= n; i ++){if(a[i].y < s.y || (a[i].y == s.y && a[i].x < s.x))s = a[i];}for(int i = 1; i <= n; i ++){if(a[i].x == s.x && a[i].y == s.y){a[i].angles  = s.angles = 0;}else {a[i].angles = angle(s,a[i]);}}sort(a+1,a+n+1,cmp);//for(int i = 1; i <= n; i ++)//cout << a[i].x << " JJJ " << a[i].y << endl;Node st[maxn];if(n == 1)return 0;if(n == 2)return 0;int len = 1;st[len ++] = a[1];st[len ++] = a[2];st[len ++] = a[3];for(int i = 4; i <= n; i ++){while(across_multi(st[len -1],st[len  -2],a[i]) <= 0)len --;st[len ++] = a[i]; }// for(int i = 1; i < len; i ++)// cout << a[i].x << " " << a[i].y << endl; double ans = 0; for(int i = 1; i < len - 1; i ++) { ans += (st[i].x * st[i + 1].y - st[i + 1].x * st[i].y) / 2;}return ans;}int main(){int Tcase;scanf("%d",&Tcase);while( Tcase --){scanf("%d",&n);for(int i = 1; i <= n; i ++)scanf("%lf%lf",&a[i].x,&a[i].y);double ans = gramham();printf("%.1f\n",ans);}return 0;}


0 0
原创粉丝点击