bzoj2338 数矩形

给出N(N≤1500)个点,求选四个点作为顶点组成矩形的最大面积,保证有解。

对每两个点连边,按边长排序,枚举等长且中点相同的边作为对角线组成矩形,计算面积取最大值。

时间复杂度O(n2logn)

#include<cstdio>
#include<algorithm>
int xs[1600],ys[1600];
long long ans=0;
struct edge{
    int x1,y1,x2,y2;
    long long len;
    int xm,ym;
    void cal(){
        xm=x1+x2,ym=y1+y2;
        int x=x1-x2,y=y1-y2;
        len=x*1ll*x+y*1ll*y;
    }
}es[2250005];
inline bool operator<(edge a,edge b){
    if(a.len<b.len)return 1;
    else if(a.len==b.len){
        if(a.xm<b.xm)return 1;
        else if(a.xm==b.xm)return a.ym<b.ym;
        else return 0;
    }
    else return 0;
}
inline long long cal(edge&a,edge&b){
    long long x=(a.x1-b.x1)*1ll*(a.y1-b.y2)-(a.x1-b.x2)*1ll*(a.y1-b.y1);
    return x<0?-x:x;
}
int p=0,n;
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d%d",xs+i,ys+i);
    for(int i=0;i<n;i++)for(int j=i+1;j<n;j++){
        edge&e=es[p++];
        e.x1=xs[i],e.y1=ys[i];
        e.x2=xs[j],e.y2=ys[j];
        e.cal();
    }
    std::sort(es,es+p);
    for(int i=0;i<p;i++){
        for(int j=i+1;j<p&&es[i].len==es[j].len&&es[i].xm==es[j].xm&&es[i].ym==es[j].ym;j++){
            long long s=cal(es[i],es[j]);
            if(s>ans)ans=s;
        }
    }
    printf("%lld",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/ccz181078/p/5136009.html