POJ 3348 求凸包面积

简介:

题意给出n个点求凸包,然后求出凸包面积就可以。

模板题求出凸包后用叉积求出面积就可以了。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef int PointType;
struct point
{
    PointType x,y;
    int num;
};
point data[10005],stack[10005],MinA;
int top;
PointType Direction(point pi,point pj,point pk) //判断向量PiPj在向量PiPk的顺逆时针方向 +顺-逆0共线
{
    return (pj.x-pi.x)*(pk.y-pi.y)-(pk.x-pi.x)*(pj.y-pi.y);
}
PointType Dis(point a,point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(point a,point b)
{
    PointType k=Direction(MinA,a,b);
    if(k>0) return 1;
    if(k<0) return 0;
    return Dis(MinA,a)>Dis(MinA,b);
}
int ab(int a)
{
    return a>=0?a:-a;
}
void Graham_Scan(point *a,int numa)
{
    for(int i=0; i<numa; i++)
        if(a[i].y<a[0].y||(a[i].y==a[0].y&&a[i].x<a[0].x))
            swap(a[i],a[0]);
    MinA=a[0],top=0;
    sort(a+1,a+numa,cmp);
    stack[top++]=a[0],stack[top++]=a[1],stack[top++]=a[2];
    for(int i=3; i<numa; i++)
    {
        while(Direction(stack[top-2],stack[top-1],a[i])<0)
            top--;
        stack[top++]=a[i];
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%d%d",&data[i].x,&data[i].y);
        if(n<3)
        {
            puts("0");
            continue;
        }
        Graham_Scan(data,n);
        int sum=0;
        for(int i=1; i<top-1; i++)
            sum+=ab(Direction(stack[0],stack[i],stack[i+1]));
        printf("%d\n",sum/100);
    }
    return 0;
}


目录
相关文章
|
3月前
leetcode-463:岛屿的周长
leetcode-463:岛屿的周长
16 0
|
3月前
leetcode-593:有效的正方形
leetcode-593:有效的正方形
17 0
leetcode 463 岛屿的周长
leetcode 463 岛屿的周长
52 0
leetcode 463 岛屿的周长
LeetCode 221. 最大正方形
LeetCode 221. 最大正方形
57 0
LeetCode 221. 最大正方形
2021杭电多校第八场 HDU7063-Square Card(求两圆相交面积)
2021杭电多校第八场 HDU7063-Square Card(求两圆相交面积)
56 0
2021杭电多校第八场 HDU7063-Square Card(求两圆相交面积)
暴力枚举:三角形的组成
题目: 给定一个n个数的数字序列,每个数不超过1e9,有Q此询问,每次询问一个区间是否存在三个数可以组成一 个三角形,输入YES或NO(1<=n,Q<=1e5);
83 0
|
人工智能 Python
LeetCode面试系列 第10天:No.976 - 三角形的最大周长
LeetCode面试系列 第10天:No.976 - 三角形的最大周长
98 0
LeetCode面试系列 第10天:No.976 - 三角形的最大周长