1069: [SCOI2007]最大土地面积
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2938 Solved: 1149
[ Submit][ Status][ Discuss]
Description
在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成
的多边形面积最大。
Input
第1行一个正整数N,接下来N行,每行2个数x,y,表示该点的横坐标和纵坐标。
Output
最大的多边形面积,答案精确到小数点后3位。
Sample Input
5
0 0
1 0
1 1
0 1
0.5 0.5
0 0
1 0
1 1
0 1
0.5 0.5
Sample Output
1.000
HINT
数据范围 n<=2000, |x|,|y|<=100000
Source
题解:旋转卡壳
枚举对角线,在两侧求最大面积三角形。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 2003
#define eps 1e-7
using namespace std;
struct vector{
double x,y;
vector (double X=0,double Y=0){
x=X,y=Y;
}
}a[N],ch[N];
int n,m,cnt;
double ans;
typedef vector point;
vector operator -(vector a,vector b){
return vector (a.x-b.x,a.y-b.y);
}
vector operator +(vector a,vector b){
return vector (a.x+b.x,a.y+b.y);
}
vector operator *(vector a,double t){
return vector (a.x*t,a.y*t);
}
bool operator <(vector a,vector b){
return a.x<b.x||a.x==b.x&&a.y<b.y;
}
int dcmp(double x)
{
if (fabs(x)<eps) return 0;
return x<0?-1:1;
}
double cross(vector a,vector b){
return a.x*b.y-a.y*b.x;
}
double dot(vector a,vector b){
return a.x*b.x+a.y*b.y;
}
void convexhull()
{
sort(a+1,a+n+1);
m=0;
if (n==1){
ch[m++]=a[1];
return;
}
for (int i=1;i<=n;i++){
while (m>1&&cross(ch[m-1]-ch[m-2],a[i]-ch[m-2])<=0) m--;
ch[m++]=a[i];
}
int k=m;
for (int i=n-1;i>=1;i--){
while (m>k&&cross(ch[m-1]-ch[m-2],a[i]-ch[m-2])<=0) m--;
ch[m++]=a[i];
}
m--;
}
void rotating()
{
int i,j,k,k1;
for (int i=0;i<m-2;i++){
k=(i+3)%m; k1=(i+1)%m;
for (int j=i+2;j<m;j++){
while (fabs(cross(ch[j]-ch[i],ch[k]-ch[i]))<fabs(cross(ch[j]-ch[i],ch[(k+1)%m]-ch[i]))&&(k+1)%m!=i) k=(k+1)%m;
while (fabs(cross(ch[j]-ch[i],ch[k1]-ch[i]))<fabs(cross(ch[j]-ch[i],ch[(k1+1)%m]-ch[i]))&&(k1+1)%m!=j) k1=(k1+1)%m;
ans=max(ans,fabs(cross(ch[j]-ch[i],ch[k]-ch[i]))+fabs(cross(ch[j]-ch[i],ch[k1]-ch[i])));
}
}
}
int main()
{
freopen("land.in","r",stdin);
freopen("land.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
convexhull();
/*if (m<4) {
printf("0.000\n");
return 0;
}*/
//for (int i=0;i<m;i++) cout<<ch[i].x<<" "<<ch[i].y<<endl;
ans=0;
rotating();
printf("%.3lf\n",ans/2.0);
}