BZOJ 1069 最大土地面积 (围成最大面积 计算几何)

1069: [SCOI2007]最大土地面积

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 3526 Solved: 1403
[Submit][Status][Discuss]
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 <vector>  
#include <cmath>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

struct point {
    double x, y;
    point(){}
    point(double x, double y):x(x),y(y){}
};

bool operator<(const point &r, const point &s) {
    return r.x < s.x || (r.x == s.x && r.y < s.y);
}
point operator+(const point &r, const point &s) {
    return point(r.x + s.x, r.y + s.y);
}
point operator-(const point &r, const point &s) {
    return point(r.x - s.x, r.y - s.y);
}
point operator*(const point &r, double s) {
    return point(r.x * s, r.y * s);
}
point operator/(const point &r, double s) {
    return point(r.x / s, r.y / s);
}
double cross(const point &r, const point &s) {
    return r.x * s.y - s.x * r.y;
}
double dot(const point &r, const point &s) {
    return r.x * s.x + r.y * s.y;
}

int n;

double area(const point &a, const point &b, const point &c) {//三角形面积 
    return fabs(cross(b - a, c - a)) / 2.0;
}
bool onleft(const point &a, const point &b, const point &p) {
    return cross(b - a, p - a) > 0;
}

point stk[2010];

int Tubao(point *pts, int n) {//ans一定在凸包上 
    sort(pts, pts+n);
    int m = 0;
    for(int i=0; i<n; i++) {
        while(m>1 && !onleft(stk[m-1], stk[m], pts[i])) m--;
        stk[++m] = pts[i];
    }
    int k = m;
    for(int i=n-2; i>=0; i--) {
        while(m>k && !onleft(stk[m-1], stk[m], pts[i])) m--; 
        stk[++m] = pts[i];
    }
    if(n > 1) m--;
    return m;
}

double maxarea(point *p, int n) {//固定ij对角线,面积法寻找max面积(对于对角线一边的三角形面积是单峰函数) 
    int next[2010];
    for(int i=1; i<=n; i++) next[i] = i % n + 1;
    if(n <= 2) return 0.0;
    if(n == 3) return area(p[0], p[1], p[2]);
    double ans = 0.0;
    for(int i = 0; i < n; i++) 
        for(int j = next[next[i]], a = next[i], b = next[j]; next[j] != i; j = next[j]) {
            while(area(p[i],p[a],p[j]) < area(p[i],p[next[a]],p[j])) a = next[a];
            while(area(p[j],p[b],p[i]) < area(p[j],p[next[b]],p[i])) b = next[b];
            ans = max(ans, area(p[i],p[a],p[j]) + area(p[j],p[b],p[i]));
        }
    return ans;
}

point p[2010];

int main() {
    int n; scanf("%d", &n);
    for(int i=0; i<n; i++) {
        double x, y; scanf("%lf%lf", &x, &y);
        p[i] = point(x, y);
    }
    int m = Tubao( p , n );
    printf("%.3f\n", maxarea(stk, m));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值