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

题目大意:

在某块平面土地上有 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 
#include 
#include 
#include 
#include 
#include 
#include 
 
#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;
}


你可能感兴趣的:(C++,勾股/叉积/向量,凸包)