[BZOJ1069][SCOI2007]最大土地面积

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1069

如果枚举四边形的四条边,时间复杂度为O(n^4),无法承受。可以换个角度,枚举四边形的对角线,在对角线两侧求出最大三角形面积并相加,则降为O(n^3)。

若使用旋转卡壳法,时间复杂度降为O(n^2),可参考白书4.3.3。

附AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<algorithm>
#define maxn 2010
using namespace std;
int n,m;
struct point{
double x,y;
bool operator<(const point&a)const{
if(a.x!=x)return x<a.x;
return y<a.y;
}
}p[maxn],ch[maxn];
point operator-(const point &a,const point &b){
return (point){a.x-b.x,a.y-b.y};
}
double cross(point a,point b){
return a.x*b.y-b.x*a.y;
}
double area(point a,point b,point c,point d){
return fabs(cross(b-a,c-a))/2+fabs(cross(d-a,d-c))/2;
}
void convexhull(){
sort(p,p+n);
for(int i=0;i<n;i++){
while(m>1&&cross(p[i]-ch[m-1],ch[m-1]-ch[m-2])>=0)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--){
while(m>k&&cross(p[i]-ch[m-1],ch[m-1]-ch[m-2])>=0)m--;
ch[m++]=p[i];
}
m--;
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
if(n<4){
puts("0.000");
return 0;
}
for(int i=0;i<n;i++){
cin>>p[i].x>>p[i].y;
}
convexhull();
double ans=0;
for(int i=0;i<m;i++){
int v1=i,v2=(i+2)%m;
for(int j=v2;(j-i+m)%m!=m/2+1;j=(j+1)%m){
point dia=ch[j]-ch[i];
// cout<<i<<' '<<j<<endl;
while(cross(ch[v1+1]-ch[v1],dia)>0)v1=(v1+1)%m;
while(cross(ch[v2+1]-ch[v2],dia)<0)v2=(v2+1)%m;
ans=max(ans,area(ch[i],ch[v1],ch[j],ch[v2]));
}
}
cout<<fixed<<setprecision(3)<<ans<<endl;
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值