[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

转载于:https://www.cnblogs.com/acjiumeng/p/7612000.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值