凸包+区间DP
第一次写区间DP的题,也是第一次写计算几何的题,找了一些模板用
写完还是不太清楚区间DP具体是什么分类
我写的是递归,他是递推
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
const int N=300+10;
const int INF=0x7f7f7f7f;
//Point定义
const double eps=1e-8;
const double PI=acos(-1.0);
int sgn(double x)
{
if(fabs(x)<eps) return 0;
if(x<0) return -1;
else return 1;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x=_x;y=_y;
}
Point operator - (const Point &b) const
{
return Point(x-b.x,y-b.y);
}
//叉积
double operator ^ (const Point &b) const
{
return x*b.y-y*b.x;
}
//点积
double operator * (const Point &b) const
{
return x*b.x+y*b.y;
}
///绕原点旋转角度B(弧度值),后x,y的变化
void transXY(double B)
{
double tx=x,ty=y;
x=tx*cos(B)-ty*sin(B);
y=tx*sin(B)+ty*cos(B);
}
};
//相对于Point p0的极角排序
Point p0;
bool _cmp(Point p1,Point p2)
{
double tmp=(p1-p0)^(p2-p0);
if(sgn(tmp)>0) return true;
return false;
}
//判断凸多边形
//允许共线边
//点可以是顺时针给出也可以是逆时针给出
//点的编号0~n-1
bool isconvex(Point poly[],int n)
{
bool s[3];
memset(s,false,sizeof(s));
for(int i=0;i<n;i++)
{
s[sgn((poly[(i+1)%n]-poly[i])^(poly[(i+2)%n]-poly[i]))+1]=true;
if(s[0]&&s[2]) return false;
}
return true;
}
int calc(Point a,Point b,int p)
{
return (int)(abs(a.x+b.x)*abs(a.y+b.y))%p;
}
Point a[N];
int cost[N][N],d[N][N];
int n,p;
int dp(int l,int r)
{
if(d[l][r]!=-1) return d[l][r];
if(r-l<=2) return 0;
d[l][r]=INF;
for(int i=l+1;i<r;i++)
d[l][r]=min(d[l][r],dp(l,i)+dp(i,r)+cost[l][i]+cost[i][r]);
return d[l][r];
}
int main()
{
while(~scanf("%d%d",&n,&p))
{
int x,y;
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
a[i]=Point(x,y);
}
int k=0;
p0=a[0];
for(int i=1;i<n;i++) //找出最下边的点
{
if((p0.y>a[i].y)||(p0.y==a[i].y&&p0.x>a[i].x))
{
p0=a[i];
k=i;
}
}
a[k]=a[0];
a[0]=p0;
sort(a+1,a+n,_cmp); //顺时针排序
if(isconvex(a,n))
{
for(int i=0;i<n;i++)
for(int j=i+2;j<n;j++)
{
cost[i][j]=cost[j][i]=calc(a[i],a[j],p);
}
memset(d,-1,sizeof(d));
printf("%d\n",dp(0,n-1));
}
else
printf("I can't cut.\n");
}
return 0;
}