1069: [SCOI2007]最大土地面積|旋轉卡殼

旋轉卡殼就是先求出凸包,然後在凸包上枚舉四邊形的對角線兩側分別找面積最大的三角形
由於在兩側找面積最大的三角形的頂點是單調的所以複雜度就是n2
單調的這個性質可以自行畫圖感受一下,似乎比較顯然

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define N 2002
using namespace std;
struct W{double x,y;}a[N],st[N];
int n,top;
W operator-(W a,W b)
{
    return (W){a.x-b.x,a.y-b.y};
}
double dis(W a,W b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double operator*(W a,W b)
{
    return a.x*b.y-a.y*b.x;
}
bool operator<(W c,W d)
{
    double t=(c-a[1])*(d-a[1]);
    if(t==0)return dis(c,a[1])<dis(d,a[1]);
    return t<0;
}
void Graham()
{
    int k=1;
    for(int i=2;i<=n;i++)
        if(a[k].y>a[i].y||(a[k].y==a[k].y&&a[k].x>a[i].x))
            k=i;
    swap(a[k],a[1]);sort(a+1,a+n+1);
    st[++top]=a[1],st[++top]=a[2];
    for(int i=3;i<=n;i++)
    {
        while(top>1&&(a[i]-st[top-1])*(st[top]-st[top-1])<=0)top--;
        st[++top]=a[i];
    }
}
double Rotating_caliper()
{
    double mx=0;st[top+1]=a[1];
    int a,b;
    for(int x=1;x<=top;x++)
    {
        a=x%top+1,b=(x+2)%top+1;
        for(int y=x+2;y<=top;y++)
        {
            while(a%top+1!=y&&(st[y]-st[x])*(st[a+1]-st[x])>(st[y]-st[x])*(st[a]-st[x]))a=a%top+1;
            while(b%top+1!=x&&(st[b+1]-st[x])*(st[y]-st[x])>(st[b]-st[x])*(st[y]-st[x]))b=b%top+1;
            mx=max(mx,(st[y]-st[x])*(st[a]-st[x])+(st[b]-st[x])*(st[y]-st[x]));
        }
    }
    return mx;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lf%lf",&a[i].x,&a[i].y);
    Graham();
    printf("%.3lf",Rotating_caliper()/2);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章