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<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef double db;
const int N = 2000 + 10;
struct P{
db x, y;
P(){};
P(db a, db b){x = a, y = b;}
}p[N], s[N];
int n, top;
db ans;
db dis(P a, P b){
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
P operator-(P a, P b){
return P(a.x - b.x, a.y - b.y);
}
db operator*(P a, P b){
return a.x * b.y - a.y * b.x;
}
bool operator<(P a, P b){
db t = (a - p[1]) * (b - p[1]);
return t > 0 || t == 0 && dis(p[1], a) < dis(p[1], b);
}
void graham(){
int k = 1;
for(int i = 2; i <= n; i++)
if(p[k].y > p[i].y || p[k].y == p[i].y && p[k].x > p[i].x)
k = i;
swap(p[k], p[1]);
sort(p + 2, p + n + 1);
s[++top] = p[1]; s[++top] = p[2];
for(int i = 3; i <= n; i++){
while(top > 1 && (s[top] - s[top-1]) * (p[i] - s[top-1]) <= 0)
top--;
s[++top] = p[i];
}
}
db RotatingCaliper(){
s[top+1] = p[1];
int a, b;
for(int i = 1; i <= top; i++){
a = i % top + 1; b = (i + 2) % top + 1;
for(int j = i + 2; j <= top; j++){
while(a % top + 1 != j && (s[a] - s[i])*(s[j] - s[i]) < (s[a+1] - s[i])*(s[j] - s[i]))
a = a % top + 1;
while(b % top + 1 != j && (s[j] - s[i])*(s[b] - s[i]) < (s[j] - s[i])*(s[b+1] - s[i]))
b = b % top + 1;
ans = max(ans, (s[a] - s[i])*(s[j] - s[i]) + (s[j] - s[i])*(s[b] - s[i]));
}
}
return ans;
}
void init(){
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
}
void work(){
graham();
printf("%.3lf\n", RotatingCaliper() / 2);
}
int main(){
init();
work();
return 0;
}