不说区间dp还真不造要往dp这边想。。
求完凸包每个多边形可以用2个端点表示,那么设d[i][j]为以ij为端点的凸多边形剖分完的最小代价
为了使转移看起来好看点这里就不把边ij算在内
d[i][j]=min{d[i][k]+d[k][j]+a[i][k]+a[k][j]} k=i+1...j-1
这就变成了水dp了嘛。。
然后区间dp用记忆化搜索写起来十分舒服,比以前的写法要轻松很多,以后都用这种新姿势吧(记忆化搜索是个好东西=v=
然后因为2个abs少写了一个查了半天qaq菜蛙!!
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 305
#define nm 1000005
#define pi 3.1415926535897931
using namespace std;
const int inf=1000000007;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
struct P{
int x,y;
P(int x=0,int y=0):x(x),y(y){}
P operator-(const P&o){return P(x-o.x,y-o.y);}
int operator*(const P&o){return x*o.y-y*o.x;}
bool operator<(const P&o){return x<o.x||(x==o.x&&y<o.y);}
}p[NM],s[NM];
int n,mod,a[NM][NM],d[NM][NM],m;
bool v[NM][NM];
void gra(){
sort(p+1,p+1+n);
inc(i,1,n){
while(m>1&&(p[i]-s[m-1])*(s[m]-s[m-1])>=0)m--;
s[++m]=p[i];
}
int k=m;
dec(i,n-1,1){
while(m>k&&(p[i]-s[m-1])*(s[m]-s[m-1])>=0)m--;
s[++m]=p[i];
}
m--;
}
int dfs(int x,int y){
if(v[x][y])return d[x][y];
v[x][y]++;if(y-x<=2)return 0;
d[x][y]=inf;
inc(k,x+1,y-1)d[x][y]=min(d[x][y],dfs(x,k)+dfs(k,y)+a[x][k]+a[k][y]);
//printf("%d %d:%d\n",x,y,d[x][y]);
return d[x][y];
}
int main(){
//freopen("data.in","r",stdin);
while(~scanf("%d%d",&n,&mod)){
inc(i,1,n)p[i].x=read(),p[i].y=read();
m=0;mem(a);mem(d);mem(v);gra();
if(m<n){printf("I can't cut.\n");continue;}
inc(i,1,n)inc(j,1,n)if(abs(j-i)>1)a[i][j]=abs(s[i].x+s[j].x)*abs(s[i].y+s[j].y)%mod;
printf("%d\n",dfs(1,n));
}
return 0;
}
You want to hold a party. Here's a polygon-shaped cake on the table. You'd like to cut the cake into several triangle-shaped parts for the invited comers. You have a knife to cut. The trace of each cut is a line segment, whose two endpoints are two vertices of the polygon. Within the polygon, any two cuts ought to be disjoint. Of course, the situation that only the endpoints of two segments intersect is allowed.
The cake's considered as a coordinate system. You have known the coordinates of vexteces. Each cut has a cost related to the coordinate of the vertex, whose formula is costi, j = |xi + xj| * |yi + yj| % p. You want to calculate the minimum cost.
NOTICE: input assures that NO three adjacent vertices on the polygon-shaped cake are in a line. And the cake is not always a convex.
Input
There're multiple cases. There's a blank line between two cases.The first line of each case contains two integers, N and p (3 ≤ N, p ≤ 300), indicating the number of vertices. Each line of the following N lines contains two integers, x and y (-10000 ≤ x, y ≤ 10000), indicating the coordinate of a vertex. You have known that no two vertices are in the same coordinate.
Output
If the cake is not convex polygon-shaped, output "I can't cut.". Otherwise, output the minimum cost.
Sample Input
3 3 0 0 1 1 0 2
Sample Output
0
Author: LI, Zezhou
Contest: ZOJ Monthly, September 2011