题意:求最小外接矩阵
思路:
相当于求凸包的最小外接矩阵
有一条边一定在凸包上,再根据旋转卡壳的性质,O(n)求解
两题代码基本上没什么差别
关于旋转卡壳的总结 , 传送门
#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+6;
const int MOD=1e9+7;
template <class T>
bool sf(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
int sign(double x){
return abs(x)<1e-7?0:x<0?-1:1;
}
struct Point{
double x,y;
Point(double x=0, double y=0) : x(x), y(y) {}
Point operator - (const Point &rhs) const{
return Point(x-rhs.x,y-rhs.y);
}
bool operator == (const Point &rhs) const{
return sign(x-rhs.x)==0&&sign(y-rhs.y)==0;
}
bool operator < (const Point &rhs)const{
if(x==rhs.x) return y<rhs.y;
else return x<rhs.x;
}
}p[N];
typedef Point Vector;
double cross(Vector A,Vector B){
return A.x*B.y-A.y*B.x;
}
int n;
typedef vector<Point> Polygon;
Polygon convex_hull(Polygon P){
sort(P.begin(),P.end());
P.erase(unique(P.begin(), P.end()), P.end());
int n=P.size(),k=0;
Polygon Q(n*2);
for(int i=0;i<n;++i){
while(k>1&&cross(Q[k-2]-Q[k-1],Q[k-2]-P[i])<=0) k--;
Q[k++]=P[i];
}
int t=k;
for(int i=n-2;i>=0;--i){
while(k>t && cross(Q[k-2]-Q[k-1],Q[k-2]-P[i])<=0) k--;
Q[k++]=P[i];
}
Q.resize(k-1);
return Q;
}
double dis(Point a,Point b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int ks=0;
double dot(Point a,Point b){
return a.x*b.x+a.y*b.y;
}
double rc(Polygon t){
int sz=(int)t.size();
// for(auto tt :t ) cout <<tt.x<<" ~ "<<tt.y<<endl;
t.pb(t[0]);
int l=1,r=1,up=2;
double res=1e20;
for(int i=0;i<sz;++i){ /// 枚举边
while(sign(dot(t[r+1]-t[i],t[i+1]-t[i])- dot(t[r]-t[i],t[i+1]-t[i]))>=0) r++,r%=sz;
while(sign(cross(t[up+1]-t[i],t[i+1]-t[i])- cross(t[up]-t[i],t[i+1]-t[i]))<=0) up++,up%=sz;
if(i==0) l=r; /// Important
while(sign(dot(t[l+1]-t[i],t[i+1]-t[i])- dot(t[l]-t[i],t[i+1]-t[i]))<=0) l++,l%=sz;
double dis1=sqrt(dis(t[i],t[i+1]));
double dis2=(dot(t[r]-t[i],t[i+1]-t[i])/dis1);
double dis3=(dot(t[l]-t[i],t[i+1]-t[i])/dis1);
double totdis=fabs(dis2-dis3);
double h= fabs(cross(t[i+1]-t[i],t[up]-t[i])/dis1);
res=min(res,h*totdis);
}
return res;
}
int main(void){
int n;
while(scanf("%d",&n)==1){
if(!n) break;
Polygon t;
for(int i=1;i<=n;++i){
double x,y;
scanf("%lf%lf",&x,&y);
t.pb({x,y});
}
if(n==1){
printf("0.0000\n");
continue;
}
if(n==2){
printf("0.0000\n");
continue;
}
t=convex_hull(t);
printf("%.4f\n",rc(t));
}
}