給定平面 n (≤50,000) 個點,求由其任三點組成的最大三角形面積,保留兩位小數。
對固定 i,初始 j=i+1, k=j+1。移動 j 時,k 只單向移動(不回退),利用凸包凸性保證面積單峰性。判斷條件:
cross(P[i]-P[j], P[(k+1)%m]-P[k]) < 0 時 k 前進。
每次更新答案 area2(P[i],P[j],P[k])(兩倍面積,避免浮點)。
#include <bits/stdc++.h>
using namespace std;
struct Point { double x, y; };
double cross(Point a, Point b) { return a.x*b.y - a.y*b.x; }
double area2(Point a, Point b, Point c) { return abs(cross(b-a, c-a)); }
vector<Point> convexHull(vector<Point>& p) {
sort(p.begin(), p.end(), [](Point a, Point b){ return a.x!=b.x ? a.x1 && cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) --m;
ch[m++] = p[i];
}
int k = m;
for (int i=n-2; i>=0; --i) {
while (m>k && cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) --m;
ch[m++] = p[i];
}
if (n>1) --m;
ch.resize(m);
return ch;
}
double rotatingCalipers(vector<Point>& ch) {
int m = ch.size();
if (m < 3) return 0;
double ans = 0;
for (int i=0; i> n) {
vector<Point> p(n);
for (int i=0; i> p[i].x >> p[i].y;
auto ch = convexHull(p);
printf("%.2f\n", rotatingCalipers(ch) / 2.0);
}
return 0;
}
unique 配合自定義比較printf("%.2f\n", area/2)此題是「凸包上最遠點對/最大三角形」的經典應用,掌握旋轉卡殼模板可解決一系列凸包極值問題。