题意
给你一个点集 求出能覆盖这个点集的最小矩形面积
思路
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double inf = 1.0 * 1e20;
const int MaxN = 1e6 + 5;
int n,Top;
struct Point{
double x,y;
Point(double x = 0,double y = 0):x(x),y(y){}
}p[MaxN],stack[MaxN],C;
typedef Point Vector;
int dcmp(double x){
if(fabs(x) < eps) return 0;
else return x < 0 ? -1:1;
}
Vector operator + (Vector A,Vector B){ return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B){ return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double p){ return Vector(A.x*p,A.y*p);}
bool operator == (const Point &a,const Point &b){
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
double Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y;}
double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}
double dis(Point A,Point B){
return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
double compare(Point a,Point b,Point c){
return Cross(c - a,c - b);
}
bool cmp(Point a,Point b){
if(dcmp(compare(a,b,C)) == 0) return dis(a,C) < dis(b,C);
return dcmp(compare(a,b,C)) > 0;
}
void Graham_scan(Point p[]){
int i , k = 0;
Top = 2;
for(i = 1;i < n; i++){
if(p[i].y < p[k].y || (p[i].y == p[k].y && p[i].x < p[k].x)) k = i;
}
swap(p[0],p[k]);
C = p[0];
sort(p + 1,p + n,cmp);
stack[0] = p[0];
stack[1] = p[1];
stack[2] = p[2];
for(int i = 3;i < n; i++){
while(Top > 1 && compare(p[i],stack[Top],stack[Top - 1]) >= 0) Top--;
stack[++Top] = p[i];
}
//[0,Top]
}
bool Collinear(Point P1,Point P2,Point P3,Point P4){
double A = Cross(P1 - P2,P3 - P2);
double B = Cross(P1 - P2,P4 - P2);
if(!dcmp(A) && !dcmp(B)) return 1;
return 0;
}
bool Coll(Point a[],int m){//存在不共线
for(int i = 0;i < m - 1; i++){
if(!Collinear(a[i],a[i + 1],a[1],a[m - 1])) return 1;
}
return 0;
}
double RotatingCalipers(Point a[],int m){
if(m < 3 || !Coll(a,m)) return 0.0;
a[m] = a[0];
int l = 1,r = 1,up = 2;
double ans = inf;
for(int i = 0;i < m; i++){
while(dcmp(Dot(a[r + 1] - a[i],a[i + 1] - a[i]) - Dot(a[r] - a[i],a[i + 1] - a[i])) >= 0){
r = (r + 1) % m;
}//找r
while(dcmp(Cross(a[up + 1] - a[i],a[i + 1] - a[i]) - Cross(a[up] - a[i],a[i + 1] - a[i])) <= 0){
up = (up + 1) % m;
}//找up
if(i == 0) l = r;
while(dcmp(Dot(a[l + 1] - a[i],a[i + 1] - a[i]) - Dot(a[l] - a[i],a[i + 1] - a[i])) <= 0){
l = (l + 1) % m;
}//找l
double d = dis(a[i],a[i + 1]);
double h = fabs(Cross(a[up] - a[i],a[i + 1] - a[i]) / d);
double d1 = Dot(a[l] - a[i],a[i + 1] - a[i]) / d;
double d2 = Dot(a[r] - a[i],a[i + 1] - a[i]) / d;
double totdis = fabs(d2 - d1);
ans = min(ans,h * totdis);
}
return ans;
}
int main()
{
while(~scanf("%d",&n) && n){
for(int i = 0; i < n; i++){
scanf("%lf %lf",&p[i].x,&p[i].y);
}
if(n == 2 || n == 1){
printf("0.0000\n");
continue;
}
Graham_scan(p);
double ans = RotatingCalipers(stack,Top + 1);//Top + 1个点
printf("%.4f\n",ans);
}
return 0;
}