题意:输入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; }