【旋转卡壳】【bzoj 1069】: [SCOI2007]最大土地面积

本文介绍了一种解决凸包问题的方法,并通过具体代码实现了该算法。利用点坐标计算凸包,并通过比较不同顶点来寻找最大面积的三角形。

http://www.lydsy.com/JudgeOnline/problem.php?id=1069


枚举对角线就可以啦~


//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive    Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=(l),___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=(r),___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
const double eps=1e-8;
int n;
int dcmp(double a,double b)
{
	if(a<b-eps) return -1;
	if(a>b+eps) return 1;
	return 0;
}
struct Point
{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
	bool operator <(const Point &b) const
	{
		if(x==b.x) return y<b.y;
		return x<b.x;
	}
	bool operator ==(const Point &b) const
	{
		return dcmp(x,b.x)==0&&dcmp(y,b.y)==0;
	}
}pt[2020],s[2020];
/////////////////////////////////////////////////
inline double Cross(const Point&a,const Point&b) { return a.y*b.x-a.x*b.y; }  
double cross(const Point &a,const Point &b){return a.y*b.x-a.x*b.y;}
Point operator-(const Point &a,const Point &b){return Point(a.x-b.x,a.y-b.y);}
int ConvexHull()
{
	sort(&pt[0],&pt[n]);
	int m=0;
	rep(i,0,n-1)
	{
		while(m>1&&dcmp(cross(s[m-1]-s[m-2],pt[i]-s[m-2]),0)<=0) m--;
		s[m++]=pt[i];
	}
	int k=m;
	per(i,n-2,0)
	{
		while(m>k&&dcmp(cross(s[m-1]-s[m-2],pt[i]-s[m-2]),0)<=0) m--;
		s[m++]=pt[i];
	}
	if(n>1) m--;
	return m;
}
/////////////////////////////////////////////////
void input()
{
    n=read();
    rep(i,0,n-1) scanf("%lf%lf",&pt[i].x,&pt[i].y);
}
void solve()
{
	n=ConvexHull();
	double ans=0;
	s[n]=s[0];
	rep(i,0,n-1)
	{
		int a=(i+1)%n,b=(i+2)%n,c=(i+3)%n;
		for(;;)
		{
			while(dcmp(cross(s[b]-s[i],s[(a+1)%n]-s[a]),0)<0) a=(a+1)%n;
			while(dcmp(cross(s[b]-s[i],s[(c+1)%n]-s[c]),0)>0) c=(c+1)%n;
			double A=abs(cross(s[a]-s[i],s[b]-s[i]));
			A+=abs(cross(s[b]-s[i],s[c]-s[i]));
			if(A>ans) ans=A;
			b=(b+1)%n;
			if((b+1)%n==i) break;
		}
	}
	printf("%.3lf\n",ans*0.5);
}
/////////////////////////////////////////////////
int main()
{
    #ifndef _TEST
    freopen("std.in","r",stdin); freopen("std.out","w",stdout);
    #endif
    input(),solve();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值