1069: [SCOI2007]最大土地面积
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3720 Solved: 1471
[ 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
思路:显然该四边形位于凸包上面,那么先求出凸包点。考虑枚举对角线,然后就是裸的旋转卡壳找最大三角形面积了。
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstring>
# include <algorithm>
using namespace std;
const int maxn = 2003;
int n;
struct node{double x, y;}a[maxn], ans[maxn];
inline node operator-(node a, node b)
{
return node{a.x-b.x, a.y-b.y};
}
inline double operator*(node a, node b)
{
return a.x*b.y - a.y*b.x;
}
inline double dis(node a)
{
return a.x*a.x+a.y*a.y;
}
inline bool cmp(node x, node y)
{
if(fabs((x-a[1])*(y-a[1]))>0) return (x-a[1])*(y-a[1])>0;
return dis(x-a[1])<dis(y-a[1]);
}
inline double cha(node a, node b, node c)
{
return fabs((a-b)*(a-c))/2;
}
int main()
{
scanf("%d",&n);
for(int i=1; i<=n; ++i) scanf("%lf%lf",&a[i].x,&a[i].y);
for(int i=2;i<=n;i++)
if(a[i].y<a[1].y||(a[i].y==a[1].y&&a[i].x<a[1].x))
swap(a[1],a[i]);
sort(a+2,a+n+1,cmp);
int top=2;ans[1]=a[1];ans[2]=a[2];
for(int i=3; i<=n; ++i)
{
while(top>1&&(a[i]-ans[top-1])*(ans[top]-ans[top-1])>=0) --top;
ans[++top]=a[i];
}
for(int i=0;i<top;i++)
ans[i] = ans[i+1];
n=top;
double imax = 0;
for(int i=0;i<n-2;i++)
{
int ti=(i+3)%n, tj=(i+1)%n;
for(int j=i+2; j<n; ++j)
{
while(cha(ans[i],ans[j],ans[ti]) < cha(ans[i],ans[j],ans[(ti+1)%n])) ti=(ti+1)%n;
while(cha(ans[i],ans[j],ans[tj]) < cha(ans[i],ans[j],ans[(tj+1)%n])) tj=(tj+1)%n;
imax = max(imax, cha(ans[i],ans[j],ans[ti])+cha(ans[i],ans[j],ans[tj]));
}
}
printf("%.3f",imax);
return 0;
}