POJ - 3348 Cows

题目链接:https://vjudge.net/problem/POJ-3348

题意:每阳一头牛就要50平方米的空间,给你n个点,要你围个牧场,看最多可以养多少条牛。

思路:先根据点求出凸包,再求凸包的面积即可。

#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const double PI = atan(1.0)*4.;
const int maxn=200005;
struct point
{
    double x,y;
    point friend operator -(point A,point B)
    {
        return {A.x-B.x,A.y-B.y};
    }
};
struct line
{
    point x,y;
};
point p[maxn];//输入点集
point q[maxn];//凸包点集
double dis(point A,point B)//两点的距离
{
    return sqrt( (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y) );
}
double chaj(point A,point B)//差积
{
    return A.x*B.y-A.y*B.x;
}
bool cmp(point A,point B)//以p[0]为起点,按差积逆时针排序
{
    double s=chaj(A-p[1],B-p[1]);
    if(s>0)
        return true;
    if(s==0)
        return dis(A,p[1])<dis(B,p[1]);
    return false;
}
double area(int len)//凸包求面积
{
    if(len<3)
        return 0;
    double sum=0;
    for(int i=3;i<=len;i++)
       sum+=chaj( q[i-1]-q[1] , q[i]-q[1] );
    return fabs(sum)/2;
}
int main()
{
    int N;
    cin>>N;
    for(int i=1; i<=N; i++)
        cin>>p[i].x>>p[i].y;
    if(N<3)
    {
        cout<<"0"<<endl;
        return 0;
    }
    //找到最小的点
    int K=1;
    for(int i=2; i<=N;i++)
    {
        if(p[i].y<p[K].y||(p[i].y==p[K].y&&p[i].x<p[K].x))
            K=i;
    }
    swap(p[1],p[K]);
    sort(p+2,p+1+N,cmp);
    q[1]=p[1];
    q[2]=p[2];
    int len=2;//凸包点的个数
    for(int i=3; i<=N; i++)
    {
        while(len>=2&&chaj(q[len]-q[len-1],p[i]-q[len-1])<=0)
            len--;
        q[++len]=p[i];
    }
    if(len<3)
    {
        cout<<"0"<<endl;
        return 0;
    }
    int ans=area(len);
    cout<<ans/50<<endl;
}

 

posted @ 2020-10-13 16:56  ~zcb  阅读(122)  评论(0编辑  收藏  举报