zoukankan      html  css  js  c++  java
  • [Gym-101512C] 凸包+最远点对

    找最大的四边形或者三角形面积,先求凸包,然后枚举两个点,再通过旋转,找最大的另两个点

    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    #define pii pair<int,int>
    #define C 0.5772156649
    #define pi acos(-1.0)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    using namespace std;
    
    const double g=10.0,eps=1e-7;
    const int N=1000+10,maxn=60000+10,inf=0x3f3f3f;
    
    void debug(){cout<<-1<<endl;}
    
    struct point{
        ll x,y;
    };
    point p[N],s[N];
    int top,n;
    ll dir(point p1,point p2,point p3)
    {
        return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
    }
    ll dis(point a,point b)
    {
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    ll area(point p1,point p2,point p3)
    {
        return fabs(dir(p1,p2,p3));
    }
    bool comp(point a,point b)
    {
        ll te=dir(p[0],a,b);
        if(te<0)return 1;
        if(te==0&&dis(p[0],a)<dis(p[0],b))return 1;
        return 0;
    }
    void graham()
    {
        int pos,minx,miny;
        minx=miny=inf;
        for(int i=0;i<n;i++)
        {
            if(p[i].x<minx||(p[i].x==minx&&p[i].y<miny))
            {
                minx=p[i].x;
                miny=p[i].y;
                pos=i;
            }
        }
        swap(p[0],p[pos]);
        sort(p+1,p+n,comp);
        p[n]=p[0];
        s[0]=p[0],s[1]=p[1],s[2]=p[2];
        top=2;
        for(int i=3;i<=n;i++)
        {
            while(dir(s[top-1],s[top],p[i])>=0&&top>=2)top--;
            s[++top]=p[i];
        }
        //cout<<top<<endl;
        if(top==3)
        {
            cout<<area(s[0],s[1],s[2])/2;
            if(area(s[0],s[1],s[2])&1)cout<<".5";
            cout<<endl;
            return ;
        }
        else if(top<3)
        {
            cout<<0<<endl;
            return ;
        }
       /* for(int i=0;i<top;i++)
            cout<<s[i].x<<" "<<s[i].y<<endl;*/
        ll ans=0;
        for(int i=0;i<top;i++)
        {
            int j,a1=(i+1)%top,a2=(i+3)%top;
            for(j=(i+2)%top;j!=i;j=(j+1)%top)
            {
                while(a1!=j&&area(s[(a1+1)%top],s[i],s[j])>=area(s[a1],s[i],s[j]))a1=(a1+1)%top;
                while(a2!=i&&area(s[(a2+1)%top],s[i],s[j])>=area(s[a2],s[i],s[j]))a2=(a2+1)%top;
                ans=max(ans,area(s[a1],s[i],s[j])+area(s[a2],s[i],s[j]));
            }
        }
        cout<<ans/2;
        if(ans&1)cout<<".5";
        cout<<endl;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout<<setiosflags(ios::fixed)<<setprecision(1);
        int t;
        cin>>t;
        while(t--)
        {
            cin>>n;
            for(int i=0;i<n;i++)cin>>p[i].x>>p[i].y;
            graham();
        }
        return 0;
    }
    /********************
    3
    6
    0 0
    3 7
    10 0
    11 6
    0 10
    10 10
    5
    0 0
    -2 -2
    3 -2
    0 1
    0 3
    10
    3 1
    4 1
    5 9
    2 6
    5 3
    5 8
    9 7
    9 3
    2 3
    8 4
    ********************/
    View Code
  • 相关阅读:
    测试方案
    如何编写一个好的测试计划
    一个好的测试过程
    java后台生成echarts图表并保存图片
    Javascript数组排序,并获取排序后位置对应的原索引(堆排序实现)
    修改tomcat编码方式
    json序列化反序列化后function丢失
    Mysql数据库存取性能优化
    java创建文件
    Java POI导出ppt简单实现
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/7612000.html
Copyright © 2011-2022 走看看