Problem Description
Maxwell is a naughty boy.
One day, he fouled the white and clean wall with ink. Since his mother will come back home soon, Maxwell wanted to find a white convex polygon to cover these ink drops. Could you tell him the minimal area of the convex polygon which can cover all the ink drops?
Now, given the coordinates of these ink drops, you will answer the minimal area of the convex polygon that can cover all the ink drops.
Input
The first line of the input is a positive integer T. T is the number of the test cases followed.
The first line of each test case is a positive integer N (0<N<=10^5). N is the number of the ink drops. After that, N lines are followed. The ith line contains two integers Xi and Yi (0<=Xi, Yi<20000) which tell you the coordinate of the ith ink drops. There may be one or more spaces between these integers.
Output
The output of the program should consist of one line of output for each test case. The output of each test case only contains the minimal area of the convex polygon. The area is a real number that has one digit after the decimal point. No redundant spaces are needed.
Sample Input
2
4
0 0
1 0
0 1
1 1
2
0 0
0 1
Sample Output
1.0
0.0
//题意:给出N个点,求这N个点围成的最小面积。
//题解:求凸多边形面积,用凸包面积模板。
//标程:
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const double eps = 1e-8;
const double inf = 10000000000.0;
const int Max = 100010; //范围1到100000;
int stk[Max], top;
struct Point
{
double x, y;
} p[Max];
int dblcmp(double k)
{
if (fabs(k) < eps) return 0;
return k > 0 ? 1 : -1;
}
double multi(Point p0, Point p1, Point p2) // 叉积求面积;
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double getDis(Point a, Point b) //求两点之间的距离;
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(const Point& a, const Point& b)
{
int d = dblcmp(multi(p[0], a, b));
if (!d) return getDis(p[0], a) < getDis(p[0], b);
return d > 0;
}
int main()
{
// freopen("a.txt","r",stdin);
int n, i, k, t;
double tx, ty;
cin >> t;
while (t --)
{
memset(stk,0,sizeof(stk));
scanf("%d", &n);
tx = ty = inf;
for (i = 0; i < n; i++)
{
scanf ("%lf%lf", &p[i].x, &p[i].y);
int d = dblcmp(ty-p[i].y);
if (!d && dblcmp(tx-p[i].x) > 0)
{
k = i; tx = p[i].x;
}
else if (d > 0)
{
k = i;
ty = p[i].y;
tx = p[i].x;
}
}
if (n <= 2)
{
printf ("0.0\n");
continue;
}
p[k].x = p[0].x;
p[k].y = p[0].y;
p[0].x = tx;
p[0].y = ty;
sort(p+1, p+n, cmp);
stk[0] = 0;
stk[1] = 1;
top = 1;
for (i = 2; i < n; i++)
{
while(top >= 1 && dblcmp(multi(p[stk[top-1]], p[i], p[stk[top]])) >= 0)
top--;
stk[++top] = i;
}
double area = 0;
for (i = 1; i < top; i++)
area += fabs(multi(p[stk[0]], p[stk[i]], p[stk[i+1]]));
printf ("%.1f\n", area/2);
}
return 0;
}
凸边形外壳
最新推荐文章于 2021-01-03 14:09:26 发布