Bzoj P1069 [SCOI2007]最大土地面积___凸包+旋转卡壳

15 篇文章 1 订阅
3 篇文章 0 订阅

题目大意:

在某块平面土地上有 N N N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成
的多边形面积最大。

N ≤ 2000 , ∣ x ∣ , ∣ y ∣ ≤ 100000 N≤2000,|x|,|y|≤100000 N2000,x,y100000

分析:

求出凸包,
枚举凸包中对角线,
以对角线为底,求出两边的最大面积三角形的面积 a r e a l , a r e a r area_l,area_r areal,arear
然后求一个 m a x ( a r e a l + a r e a r ) max(area_l+area_r) max(areal+arear)

代码:

 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <algorithm>
 
#define inf 0x3f3f3f3f
#define N 2005
 
using namespace std;
 
struct Node { double x, y; }a[N];
int wxy[N], n, cnt = 0; 
double Answer;
 
double chaji(int pq, int p, int q)
{
    return (a[p].x - a[pq].x) * (a[q].y - a[pq].y) - (a[q].x - a[pq].x) * (a[p].y - a[pq].y);
}
 
double juli(int p, int q)
{
    return sqrt((a[p].x - a[q].x) * (a[p].x - a[q].x) + (a[p].y - a[q].y) * (a[p].y - a[q].y));
     
}
 
void Get_tb()
{
    double p = inf, q = inf, rp;
    int useid = 0;
    for (int i = 1; i <= n; i++)
        if (p > a[i].x || (p == a[i].x && q > a[i].y)) useid = i, p = a[i].x, q = a[i].y;
    wxy[++cnt] = useid;
    int now = 0, orz = 0;
    while (now != useid)
    {
        if (!now) now = useid;
        for (int i = 1; i <= n; i++)
            if (i != now) { orz = i; break; }
        for (int i = 1; i <= n; i++)
        {
            rp = chaji(now, orz, i);
            if (rp < 0 || (rp == 0 && juli(now, i) > juli(now, orz))) orz = i;
        }
        wxy[++cnt] = orz;
        now = orz;
    }
    --cnt;
}
 
void Get_Answer()
{
    for (int i = 1; i <= cnt; i++)
    {
        int id1 = i + 1; if (id1 > cnt) id1 -= cnt; 
        int id2 = i + 3; if (id2 > cnt) id2 -= cnt;
        for (int j = i + 2; j <= cnt; j++)
        {
            while ((fabs(chaji(wxy[i], wxy[j], wxy[id1])) < fabs(chaji(wxy[i], wxy[j], wxy[id1 % cnt + 1]))) && (id1 % cnt + 1 != j)) 
                id1 = id1 % cnt + 1;    
            while ((fabs(chaji(wxy[i], wxy[j], wxy[id2])) < fabs(chaji(wxy[i], wxy[j], wxy[id2 % cnt + 1]))) && (id2 % cnt + 1 != i)) 
                id2 = id2 % cnt + 1;
            double area = fabs(chaji(wxy[i], wxy[j], wxy[id1])) / 2 + fabs(chaji(wxy[i], wxy[j], wxy[id2])) / 2;
            if (area > Answer) Answer = area;
        }   
    }   
}
 
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%lf %lf", &a[i].x, &a[i].y); 
    Get_tb();
    Get_Answer();
    printf("%.3lf\n", Answer);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值