题意
给你一个原点,再给你一些其他的点,给你一些橡皮筋,让你围成一些凸包,且每个凸包必须包含原点,且互相之间除了原点互不相交。
问你最少围成面积是多少
题解
以原点为中心,极角排序一下
设dp【k】【i】【j】表示我用k个橡皮筋包含从i到j的点的最少围成面积
易有dp【k】【i】【j】=dp【k-1】【i】【d】+dp【1】【d+1】【j】
复杂度为O(kn^3)
代码
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include<cstdio>
using namespace std;
const double eps=1e-10;
const double PI=acos(-1.0);
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator -(Point a,Point b){
return Vector(a.x-b.x,a.y-b.y);
}
Vector operator +(Point a,Point 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);
}
Vector operator /(Vector a,double p){
return Vector(a.x/p,a.y/p);
}
bool operator <(const Point& a,const Point& b){
return a.x<b.x||(a.x==b.x&&a.y<b.y);//在有精度需求,比如使用lower_bound的时候,加上dcmp()
}
int dcmp(double x){
if(fabs(x)<eps)return 0;
if(x<0)return -1;
return 1;
}
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 Length(Vector a){
return sqrt(Dot(a,a));
}//长度
//返回逆时针旋转90度的单位法向量;
Vector Normal(Vector a){
double l=Length(a);
return Vector(-a.y/l,a.x/l);
}
//返回单位向量;
Vector normal(Vector a){
double l=Length(a);
return Vector(a.x/l,a.y/l);
}
//返回向量夹角,无方向
double Angle(Vector a,Vector b){
return acos(Dot(a,b)/Length(a)/Length(b));
}
//逆时针旋转向量
Vector Rotate(Vector a,double rad){
return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
//求p+v*t与q+w*t的交点,使用时确保Cross(v,w)不等于0
Point GetlineIntersection(Point p,Vector v,Point q,Vector w){
Vector u=p-q;
double t=Cross(w,u)/Cross(v,w);
return p+v*t;
}
//求p到直线ab的距离
double DistanceToline(Point p,Point a,Point b){
Vector v1=p-a,v2=b-a;
return fabs(Cross(v1,v2)/Length(v2));
}
//求p到线段ab的距离
double DistanceToSegment(Point p,Point a,Point b){
if(a==b)return Length(p-a);
Vector v1=b-a,v2=p-a,v3=p-b;
if(dcmp(Dot(v1,v2)<0))return Length(p-a);
else if(dcmp(Dot(v1,v3))>0)return Length(p-b);
else return fabs(Cross(v1,v2)/Length(v1));
}
//线段a1a2与线段b1b2规范相交返回真
bool SegmenProperIntersection(Point a1,Point a2,Point b1,Point b2){
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
//点p在线段a1a2上返回真
bool OnSegment(Point p,Point a1,Point a2){
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
//点p在ab上的投影
Point GetLineProjection(Point P,Point A,Point B)
{
Vector v=B-A;
return A+v*(Dot(v,P-A)/Dot(v,v));
}
//与 x 轴的夹角,取值范围为 (-π,π]
double angle(Vector v){
return atan2(v.y,v.x);
}
//求线段a1,a2到线段b1,b2的最短距离
double disSegmenttoSegment(Point a1,Point a2,Point b1,Point b2)
{
double ans=DistanceToSegment(a1,b1,b2);
ans=min(ans,DistanceToSegment(a2,b1,b2));
ans=min(ans,DistanceToSegment(b1,a1,a2));
ans=min(ans,DistanceToSegment(b2,a1,a2));
return ans;
}
//多边形的有向面积,逆时针为正
double PolygonArea(Point *po,int n) {
double area = 0.0;
for(int i = 1; i < n-1; i++) {
area += Cross(po[i]-po[0], po[i+1]-po[0]);
}
return area * 0.5;
}
//求凸包,ch为返回凸包,m为凸包内点的数目,<=不允许点在边上,
int ConvecHull(Point* p,int n,Point* ch)
{
sort(p,p+n);
int m=0;
for(int i=0;i<n;i++){
while(m>1&&dcmp(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&&dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0))m--;//注意<=与<的区别
ch[m++]=p[i];
}
if(n>1)m--;
return m;
}
double s[105][105];
double ans[2][105][105];
double inf=99999999999;
int n,b;
Point p[105];
Point ch[105];
Point* k;
bool cmp(Point& a,Point& b)
{
return angle(a-p[0])<angle(b-p[0]);
}
double getans(int s,int e)
{
Point c[105];
int cnt=0,m;
c[cnt++]=p[0];
for(int i=s;;i=(i+1)%n){
c[cnt++]=k[i];
if(i==e)break;
}
m=ConvecHull(c,cnt,ch);
return PolygonArea(ch,m);
}
int main()
{
while(cin>>b>>n){
if(n==0&&b==0)break;
for(int i=0;i<n;i++){
cin>>p[i].x>>p[i].y;
}
sort(p+1,p+n,cmp);
k=p+1;
n--;
for(int i=0;i<n;i++){
s[i][i]=inf;
ans[0][i][i]=inf;
for(int j=(i+1)%n;j!=i;j=(j+1)%n){
if(Cross(p[0]-k[i],k[j]-p[0])<0)s[i][j]=getans(i,j);
else s[i][j]=inf;
ans[0][i][j]=s[i][j];
}
}
int now=0;
for(int k=2;k<=b;k++){
now=now^1;
for(int i=0;i<n;i++){
ans[now][i][i]=inf;
for(int j=(i+1)%n;j!=i;j=(j+1)%n){
ans[now][i][j]=inf;
for(int y=j-1;;y--){
if(y<0)y+=n;
if(y==i)break;
int pre=y-1;
if(pre<0)pre+=n;
ans[now][i][j]=min(ans[now][i][j],ans[now^1][i][pre]+s[y][j]);
}
}
}
}
double sum=ans[now][0][n-1];
for(int i=0;i<n;i++){
int j=i-1;
if(j<0)j=j+n;
sum=min(sum,ans[now][i][j]);
}
printf("%.2f\n",sum);
}
return 0;
}