• POJ


    题目链接: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;
    }
  • 相关阅读:
    苹果手机页面高度不够会导致下面fixed的按钮看不到了
    IOS系统倒计时直接到结束的问题解决
    TP6集成gatewayworker报错解决
    easywechat实现公众号支付jsapi支付
    Linux 下没有conio.h 的解决方法
    程序跳转语句
    循环控制语句
    arduino入门实践之驱动LCD12864
    Manjaro 安装MariaDB
    Arduino入门实践之人体红外感应模块
  • 原文地址:https://www.cnblogs.com/zcb123456789/p/13809500.html
Copyright © 2020-2023  润新知