poj 3348 Cows 求凸包面积

题目链接

大意:

求凸包的面积。

 

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1005;
struct point
{
    int x,y;
};
point a[maxn];
int st[maxn], top;
int cross(point p0,point p1,point p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(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=cross(a[0],p1,p2);
    if(tmp>0) return true;
    else if(tmp==0&&dis(a[0],p1)<dis(a[0],p2)) return true;
    else return false;
}
void init(int n)
{
    int i,k;
    point p0;
    scanf("%d%d",&a[0].x,&a[0].y);
    p0.x=a[0].x;
    p0.y=a[0].y;
    k=0;
    for(i=1;i<n;i++)
    {
        scanf("%d%d",&a[i].x,&a[i].y);
        if( (p0.y>a[i].y) || ((p0.y==a[i].y)&&(p0.x>a[i].x)) )
        {
            p0.x=a[i].x;
            p0.y=a[i].y;
            k=i;
        }
    }
    a[k]=a[0];
    a[0]=p0;

    sort(a+1,a+n,cmp);
}
void graham(int n)
{
    int i;
    if(n==1) {top=0;st[0]=0;}
    if(n==2)
    {
        top=1;
        st[0]=0;
        st[1]=1;
    }
    if(n>2)
    {
        for(i=0;i<=1;i++) st[i]=i;
        top=1;

        for(i=2;i<n;i++)
        {
            while(top>0&&cross(a[st[top-1]],a[st[top]],a[i])<=0) top--;
            top++;
            st[top]=i;
        }
    }
}
int main()
{
    int n, r;
    while(cin>>n) {
        init(n);
        graham(n);
        double ans = 0;
        for(int i = 0; i<top; i++) {
            ans += (a[st[i]].x*a[st[i+1]].y-a[st[i]].y*a[st[i+1]].x);
        }
        ans += a[st[top]].x*a[st[0]].y-a[st[top]].y*a[st[0]].x;
        ans /= 2;
        cout<<int(ans/50)<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yohaha/p/5257974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值