BZOJ1069: [SCOI2007]最大土地面積

易水人去,明月如霜。

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<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define maxn 2005
#define eps 1e-8
using namespace std;
int n,top;
double ans;
struct data{double x,y;}p[maxn],s[maxn];
inline double dcmp(double a)
{
	if (fabs(a)<=eps) return 0;
	else return a>0?1:-1;
}
inline double dis(data a,data b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
inline data operator -(data a,data b)
{
	return (data){a.x-b.x,a.y-b.y};
}
inline double operator *(data a,data b)
{
	return a.x*b.y-a.y*b.x;
}
inline bool operator <(data a,data b)
{
	int t=dcmp((p[1]-a)*(p[1]-b));
	if (t==0) return dis(p[1],a)<dis(p[1],b);
	else return t>0;
}
inline void solve()
{
	int t=1;
	F(i,2,n) if (p[i].y<p[t].y||(p[i].y==p[t].y&&p[i].x<p[t].x)) t=i;
	swap(p[t],p[1]);
	sort(p+2,p+n+1);
	s[++top]=p[1];s[++top]=p[2];
	F(i,3,n)
	{
		while (top>1&&dcmp((p[i]-s[top-1])*(s[top]-s[top-1]))>=0) top--;
		s[++top]=p[i];
	}
}
inline void getans()
{
	int a,b;
	s[top+1]=s[1];
	F(x,1,top-2)
	{
		a=x%top+1;b=(x+2)%top+1;
		F(y,x+2,top)
		{
			while (a%top+1!=y&&dcmp((s[a+1]-s[x])*(s[y]-s[x])-(s[a]-s[x])*(s[y]-s[x]))>0) a=a%top+1;
			while (b%top+1!=x&&dcmp((s[y]-s[x])*(s[b+1]-s[x])-(s[y]-s[x])*(s[b]-s[x]))>0) b=b%top+1;
			ans=max(ans,(s[a]-s[x])*(s[y]-s[x])+(s[y]-s[x])*(s[b]-s[x]));
		}
	}
}
int main()
{
	scanf("%d",&n);
	F(i,1,n) scanf("%lf%lf",&p[i].x,&p[i].y);
	solve();
	getans();
	printf("%.3lf\n",ans/2.0);
	return 0;
}


發佈了105 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章