BZOJ 1069: [SCOI2007]最大土地面积

1069: [SCOI2007]最大土地面积

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 3642  Solved: 1440

[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

Sample Output

1.000

HINT

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

题解

很显然最后四个点一定是在凸包上,那么先求出平面的凸包,用旋转卡壳求最大的四边形。

枚举对角线和两侧两点,可以看出两侧两点是单调移动的。

对于凸包是三角形的情况,实践证明没有该数据(唔)。

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=100005,inf=0x3f3f3f3f;
int n,top;
double ans;
struct point{
	double x,y;
}a[N],p0,st[N];
point operator-(point a,point b){
	return (point){a.x-b.x,a.y-b.y};
}
double operator*(point a,point b){
	return a.x*b.y-b.x*a.y;
}
double sqr(double x){
	return x*x;
} 
double dis(point a,point b){
	return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
bool cmp(point a,point b){
	double t=(a-p0)*(b-p0);
	if(t==0)return dis(a,p0)<dis(b,p0);
	return t>0;
}
int main(){
	scanf("%d",&n);
	p0.x=p0.y=inf;
	for(int i=1;i<=n;i++){
		scanf("%lf%lf",&a[i].x,&a[i].y);
		if(a[i].y==p0.y&&a[i].x<p0.x)p0=a[i];
		if(a[i].y<p0.y)p0=a[i];
	}
	sort(a+1,a+1+n,cmp);
	st[++top]=p0;
	for(int i=2;i<=n;i++){
		while(top>1&&(a[i]-st[top-1])*(st[top]-st[top-1])>=0)top--;
		st[++top]=a[i];
	}
	int a,b;
	for(int i=1;i+2<top;i++){
		a=i+1,b=(i+2)%top+1;
		for(int j=i+2;j<=top;j++){
			while(a+1!=j&&(st[a+1]-st[i])*(st[j]-st[i])>(st[a]-st[i])*(st[j]-st[i]))a++;
			while(b%top+1!=j&&(st[j]-st[i])*(st[b%top+1]-st[i])>(st[j]-st[i])*(st[b]-st[i]))b=b%top+1;
			ans=max(ans,(st[a]-st[i])*(st[j]-st[i])+(st[j]-st[i])*(st[b]-st[i]));
		}
	}
	printf("%.3lf
",ans/2);
	return 0;
}
原文地址:https://www.cnblogs.com/chezhongyang/p/7745964.html