最大三角形 - http://acm.hdu.edu.cn/showproblem.php?pid=2202
分析
- 顶点一定是凸包上的点
- 暴力三重循环求最大值
代码
// hdu 2202 最大三角形
#include<bits/stdc++.h>
#define LL long long
using namespace std;
#define MXN 50010
struct Point{
LL x, y;
// 与点p是否是为同一个点
bool operator==(const Point &p){
return x == p.x && y == p.y;
}
// 1:y最小,2:y相等时x最小
bool operator<(const Point &p){
if(y == p.y) return x < p.x;
return y < p.y;
}
// 点p(x,y)与点p1和p2构成的向量p->p1、p->p2的叉积
LL cross(const Point &p1, const Point &p2){
return (p1.x - x)*(p2.y - y) - (p1.y - y)*(p2.x - x);
}
};
int main(){
int n, q[MXN], l;
Point tr[MXN];
while(scanf("%d", &n) == 1){
Point p = {32768, 32768};
for(int i = 0; i < n; i++){
scanf("%lld %lld", &tr[i].x, &tr[i].y);
if(tr[i] < p) p = tr[i];
}
sort(tr, tr+n, [&p](Point &p1, Point &p2){
if(p1 == p || p2 == p) return p1 == p;
LL res = p.cross(p1, p2);
if(res) return res > 0;
return p1.y < p2.y;
});
l = 1;
q[0] = 0, q[1] = 1;
for(int i = 2; i <= n; i++){
while(tr[q[l-1]].cross(tr[q[l]], tr[(i)%n]) < 0) l--;
q[++l] = i == n ? 0 : i;
}
LL res = 0, tmp;
for(int i = 0; i <= l-3; i++){
for(int j = i+1; j <= l-2; j++){
for(int k = j+1; k <= l-1;k++){
tmp = tr[q[i]].cross(tr[q[j]], tr[q[k]]);
if(abs(tmp) > res) res = abs(tmp);
}
}
}
printf("%.2f\n", res/2.0);
}
return 0;
}