Codeforces340B Maximal Area Quadrilateral

本文介绍了一种计算平面上n个点中组成的凸四边形最大面积的方法。通过枚举对角线并找到距离该线最远的两点来确定面积。提供了完整的C++代码实现。

题意:输入n个点(n<300),找出4个点组成4边形(凸四边),问最大面积多少?

题解:一个四边形是由两个三角形组成,直接枚举对角线,接下来枚举点,记录最小的和最大的叉积,就是距离这条线最远的两个点

#include <bits/stdc++.h>
#define ll long long
#define maxn 100100
#define Vector Point
using namespace std;
double eps = 1e-10;
int dcmp(double x) { return fabs(x) < eps ? 0 : (x < 0 ? -1 : 1); }
struct Point {
    double x, y;
    Point(const Point& rhs): x(rhs.x), y(rhs.y) { } //拷贝构造函数
    Point(double x = 0.0, double y = 0.0): x(x), y(y) { }   //构造函数
    friend istream& operator >> (istream& in, Point& P) { return in >> P.x >> P.y; }
    friend ostream& operator << (ostream& out, const Point& P) { return out << P.x << ' ' << P.y; }
    friend Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); }
    friend Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); }
    friend Vector operator * (const Vector& A, const double& p) { return Vector(A.x*p, A.y*p); }
    friend Vector operator / (const Vector& A, const double& p) { return Vector(A.x/p, A.y/p); }
    friend bool operator == (const Point& A, const Point& B) { return dcmp(A.x-B.x) == 0 && dcmp(A.y-B.y) == 0; }
    friend bool operator < (const Point& A, const Point& B) { return A.x < B.x || (A.x == B.x && A.y < B.y); }
};
double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }    //叉积
Point a[maxn];
int main(){
    int n;double ans = 0;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
        if(i != j){
            double ma = -1e9, mi = 1e9;
            for(int k=0;k<n;k++)
            if(k!=i&&k!=j){
                double t = Cross(a[i]-a[j], a[k]-a[j]);
                ma = max(t, ma);
                mi = min(mi, t);
            }
            ans = max(ans, ma-mi);
        }
    }
    printf("%.9f\n", ans/2);
    return 0;
}

 

转载于:https://www.cnblogs.com/Noevon/p/8412579.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值