凸边形外壳

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值