洛谷:CF607B Zuma(dp,提高+/省选-)

题目:

在这里插入图片描述

分析:紫书上做过,但完全不会了!

在这里插入图片描述

代码:

#include<bits/stdc++.h>
using namespace std;
int m;
int A[505];
int D[505][505];
int f(int x,int y)
{
 if(x==y) return 1;
 if(x+1==y)
 {
  if(A[x]==A[y]) return 1;
  return 2;
 }
 if(D[x][y]!=-1) return D[x][y];
 if(A[x]==A[y])
 {
  D[x][y]=f(x+1,y-1);
 }
 else{
  D[x][y]=1<<30;
  for(int i=x;i<y;i++)
  D[x][y]=min(D[x][y],f(x,i)+f(i+1,y));
 }
 return D[x][y];
}
int main()
{
 cin>>m;
 for(int i=0;i<m;i++) cin>>A[i];
 memset(D,-1,sizeof(D));
 cout<<f(0,m-1);
}

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/107897189
今日推荐