1069: [SCOI2007]最大土地面积
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1677 Solved: 588
[ Submit][ Status]
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
旋转卡壳求最大四边形面积~
四边形就是两个三角形~所以做完【POJ 2079】这道题就轻而易举了,只要再维护一下线段另一端的对踵点即可。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-9
using namespace std;
struct Point
{
double x,y;
}a[50005];
int n,tail,h[50005];
bool cmp(Point a,Point b)
{
if (a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
double Cross(Point a,Point b,Point c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
void Graham()
{
sort(a+1,a+1+n,cmp);
tail=0;
for (int i=1;i<=n;i++)
{
while (tail>=2&&Cross(a[h[tail-2]],a[h[tail-1]],a[i])<eps)
tail--;
h[tail++]=i;
}
int tmp=tail;
h[tail++]=n-1;
for (int i=n-2;i;i--)
{
while (tail>tmp&&Cross(a[h[tail-2]],a[h[tail-1]],a[i])<eps)
tail--;
h[tail++]=i;
}
tail--;
}
double Rotating_calipers()
{
double ans=0.0;
for (int i=0;i<tail-2;i++)
{
int x=i+1,y=i+3;
for (int j=i+2;j<tail;j++)
{
while (fabs(Cross(a[h[i]],a[h[j]],a[h[x+1]]))>fabs(Cross(a[h[i]],a[h[j]],a[h[x]])))
x=(x+1)%tail;
while (fabs(Cross(a[h[i]],a[h[j]],a[h[y+1]]))>fabs(Cross(a[h[i]],a[h[j]],a[h[y]])))
y=(y+1)%tail;
ans=max(ans,fabs(Cross(a[h[i]],a[h[j]],a[h[x]]))+fabs(Cross(a[h[i]],a[h[j]],a[h[y]])));
}
}
return ans/2.0000;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=1;i<=n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
Graham();
printf("%.3lf\n",Rotating_calipers());
}
return 0;
}