您的位置:首页 > 其它

[SCOI2007]最大土地面积

2018-01-12 07:23 281 查看

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

Sample Output

1.000

HINT

数据范围 n<=2000, |x|,|y|<=100000

[b]首先最大的4个点一定在凸包上[/b]

[b]先把凸包求出来[/b]

[b]然后选定对角线,然后发现另外两个顶点是单调的,用旋转卡壳的思想[/b]

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct point
{
double x,y;
}p[5001],s[5001];
int n,top;
double ans,eps=1e-5;
int dcmp(double x)
{
if (x<-eps) return -1;
if (x>eps) return 1;
return 0;
}
double operator *(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
point operator -(point a,point b)
{
return (point){a.x-b.x,a.y-b.y};
}
double dist(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(point a,point b)
{
return (a.y<b.y)||(a.y==b.y&&a.x<b.x);
}
bool cmp2(point a,point b)
{
int t=dcmp((p[1]-a)*(p[1]-b));
if (t==0) return dist(p[1],a)<dist(p[1],b);
return t>0;
}
void graham()
{int i;
sort(p+1,p+n+1,cmp);
sort(p+2,p+n+1,cmp2);
s[++top]=p[1];s[++top]=p[2];
for (i=3;i<=n;i++)
{
while (top>1&&dcmp((p[i]-s[top-1])*(s[top]-s[top-1]))>=0) top--;
s[++top]=p[i];
}
}
void solve()
{int i,j;
s[top+1]=s[1];
for (i=1;i<=top-2;i++)
{
int a=i%top+1,b=(i+2)%top+1;
for (j=i+2;j<=top;j++)
{
while (a%top+1!=j&&dcmp((s[a+1]-s[i])*(s[j]-s[i])-(s[a]-s[i])*(s[j]-s[i]))>0) a=a%top+1;
while (b%top+1!=i&&dcmp((s[j]-s[i])*(s[b+1]-s[i])-(s[j]-s[i])*(s[b]-s[i]))>0) b=b%top+1;
//cout<<i<<' '<<a<<' '<<j<<' '<<b<<endl;
ans=max(ans,(s[a]-s[i])*(s[j]-s[i])+(s[j]-s[i])*(s[b]-s[i]));
}
}
}
int main()
{int i;
cin>>n;
for (i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
graham();
solve();
printf("%.3lf\n",ans/2.0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: